//从页面中获取
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";