批量下载

//从页面中获取
function DownMore() {
    var $trs = $("#noteTable").find("tr").not(":first");
    var i = 0;
    $trs.each(function () {
        var noteId = $(this).find("td[title='noteId']").text();
        var noteType = $(this).find("td[title='noteType']").text();
        var url = "";
        if (noteType == "2") {
            url = "ASHX/ObjectService.ashx?methodName=GetObjectImgDownload&tableName=GG_Notes&fileField=attach&keyField=noteId&objectId=" + noteId + "&attachName=" + i;
        } else {
            url = "ASHX/ObjectService.ashx?methodName=GetObjectFile&tableName=GG_Notes&fileField=attach&keyField=noteId&objectId=" + noteId + "";
        }
        DownURL(url);
        i++;
    });
}

function DownURL(strRemoteURL) {
    try {
        var elemIF = document.createElement("iframe");
        elemIF.src = strRemoteURL;
        elemIF.style.display = "none";
        document.body.appendChild(elemIF);
    } catch (e) {

    }

}

ASHX:

public void GetObjectImgDownload(HttpContext context)
        {
            WebReference.ObjectService.FileInfo file;
            string fileName = "photo";
            try
            {
                string tableName = context.Request.Params["tableName"];
                string fileNameField = context.Request.Params["fileNameField"];
                string fileField = context.Request.Params["fileField"];
                string keyField = context.Request.Params["keyField"];
                int objectId = int.Parse(context.Request.Params["objectId"]);
                string attachName = context.Request.Params["attachName"];
                attachName += ".jpeg";

                file = ws.GetObjectFile(tableName, fileField, keyField, objectId);
                if (!string.IsNullOrEmpty(file.fileName))
                    fileName = file.fileName;

                string contentType = file.contentType;
                

                byte[] fileStream = System.Convert.FromBase64String(file.content.Replace("data:image/jpeg;base64,", ""));

                context.Response.Buffer = true;
                context.Response.ContentType = "application/octet-stream";
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + UTF_FileName(attachName) + "\";");
                //attachment --- 作为附件下载, inline --- 在线打开
                context.Response.AddHeader("Content-Length", fileStream.Length.ToString());
                context.Response.BinaryWrite(fileStream);
                context.Response.End();
            }
            catch (Exception e)
            {
                context.Response.Write(e.Message);
            }
        }

  其他方法:

function DownURL(strRemoteURL, strLocalURL){
        try{
            var xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
            xmlHTTP.open("Get", strRemoteURL, false);
            xmlHTTP.send();
            var adodbStream = new ActiveXObject("ADODB.Stream");
            adodbStream.Type = 1;//1=adTypeBinary 
            adodbStream.Open();
            adodbStream.write(xmlHTTP.responseBody);
            adodbStream.SaveToFile(strLocalURL, 2);
            adodbStream.Close();
            adodbStream = null;
            xmlHTTP = null;
        }
        catch (e){
            window.confirm("下载URL出错!");
        }
        //window.confirm("下载完成."); 
    }

  例如要下载文件的地址为:htpp://www.baidu.com/test.rar  
我们可以使用window.open("htpp://www.baidu.com/test.rar"),  
但是该方法在火狐上没有效果的,在IE浏览器上是可以的。  

如果用window.location.href="htpp://www.baidu.com/test.rar";火狐有些版本是不支持的。  

为了解决这个问题,我们可以换做另外一种方法  
我们可以写成如下样式即可以了:  
window.location="htpp://www.baidu.com/test.rar"; 

转载于:https://www.cnblogs.com/yangfan5157/p/6273378.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值