通知浏览器下载文件而不是打开文件

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

通知浏览器下载文件而不是打开文件
通知浏览器下载文件而不是打开文件
━━━━━━━━━━━━━━━━━━━━━━━━━
需要在前台的html中进行设置,将onclick事件指向download_file函数并在download_file中调用后台DownLoadFile 函数,以此来实现a标签中下载文件而不是打开文件。

具体代码如下:
< ! - - - - - - - - - - html中调用- - - - - - - - - - - - - - >
< a href =';\ " " + "> " + obj[i].cFileOldName + "
< ! - - - - - - - - - - - - - - - -前台js - - - - - - - - - - - - - - - - - >
Function download_file(url) {
/ / alert('dasd');
If (typeof (download_file.iframe) == "undefined") {
var iframe = Document.createElement_x("iframe");
download_file.iframe = iframe;
Document.body.appendChild(download_file.iframe);
}
download_file.iframe.src = url;
download_file.iframe.style.display = "none";
}
< ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - >
/ / /后台
Private void DownLoadFile(HttpContext context, String File
NewName, String FileOldName, String Pub)
{
try
{
String fileName = FileOldName; / /客户端保存的文件名
String path = @"../FileUpLoad/" + Pub + "/" + FileNewNam e;
String filePath = context.Server.MapPath(path); / /路径
/ /以字符流的形式下载文件
System.IO.FileStream fs = New System.IO.FileStream(filePath , System.IO.FileMode.Open);
Byte[] bytes = New Byte[(Int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
context.Response.ContentType = "application/octet-stream";
/ /通知浏览器下载文件而不是打开
context.Response.AddHeader("Content-
Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fil eName, System.Text.Encoding.UTF8));
context.Response.BinaryWrite(bytes);
context.Response.Flush();
context.Response.End();
}
catch (Exception e) { }
}。

相关文档
最新文档