通知浏览器下载文件而不是打开文件
2015-03-02 12:51
375 查看
需要在前台的html中进行设置,将onclick事件指向download_file函数并在download_file中调用后台DownLoadFile函数,以此来实现a标签中下载文件而不是打开文件。
具体代码如下:
<!----------html中调用-------------->
<a href='javascript:void(0);' title='" + obj[i].cFilePath + "' onclick=\"" + "download_file('KnowledgeCheckHandler.ashx?flag=D&FileNewName=" + obj[i].cFileNewName + "&Pub=" + obj[i].cpublisher + "&FileOldName=" + obj[i].cFileOldName + "');\"" + ">" + obj[i].cFileOldName
+ "</a>
<!----------------前台js----------------->
function download_file(url) {
//alert('dasd');
if (typeof (download_file.iframe) == "undefined") {
var iframe = document.createElement("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 FileNewName, string FileOldName, string Pub)
{
try
{
string fileName = FileOldName;//客户端保存的文件名
string path = @"../FileUpLoad/" + Pub + "/" + FileNewName;
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(fileName, System.Text.Encoding.UTF8));
context.Response.BinaryWrite(bytes);
context.Response.Flush();
context.Response.End();
}
catch (Exception e) { }
}
具体代码如下:
<!----------html中调用-------------->
<a href='javascript:void(0);' title='" + obj[i].cFilePath + "' onclick=\"" + "download_file('KnowledgeCheckHandler.ashx?flag=D&FileNewName=" + obj[i].cFileNewName + "&Pub=" + obj[i].cpublisher + "&FileOldName=" + obj[i].cFileOldName + "');\"" + ">" + obj[i].cFileOldName
+ "</a>
<!----------------前台js----------------->
function download_file(url) {
//alert('dasd');
if (typeof (download_file.iframe) == "undefined") {
var iframe = document.createElement("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 FileNewName, string FileOldName, string Pub)
{
try
{
string fileName = FileOldName;//客户端保存的文件名
string path = @"../FileUpLoad/" + Pub + "/" + FileNewName;
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(fileName, System.Text.Encoding.UTF8));
context.Response.BinaryWrite(bytes);
context.Response.Flush();
context.Response.End();
}
catch (Exception e) { }
}
相关文章推荐
- php -- PHP实现点击a标签的href做链接时,直接保存文件(任何类型),而不是通过浏览器直接打开下载的文件
- 设置ngnix下的文件在浏览器端直接下载而不是被浏览器默认打开
- PHP实现点击a标签的href做链接时,直接保存文件(任何类型),而不是通过浏览器直接打开下载的文件
- 在iis中设置文件下载而不是在浏览器上打开
- 怎么解决点击下载Csv文件时在浏览器里面打开,而不是下载
- 在文件下载的时候将其保存而不是直接在浏览器上打开!
- 文件下载 浏览器直接打开文件而不是保存 相关问题
- JavaWeb文件下载,提示用户保存而不是让浏览器直接打开
- 如何使浏览器默认下载文件而不是打开文件
- 默认点击下载pdf等文件而不是在浏览器中打开
- 超链接下载文件,而不是浏览器打开哦
- .net C# 实现点击下载PDF文件(通知浏览器打开下载对话框) 不是直接打开
- 在ASP.NET中下载Text文件,而不是在浏览器中打开它
- Tomcat中点击链接不是下载文件,而是打开文件
- 使用asp下的adodb.stream 下载文件而不是打开
- 点击文件下载而不在浏览器中打开
- jsp中让IE提示文件下载,而不是直接打开
- 防浏览器直接打开文件的java下载
- java 实现文件下载的时候,避免浏览器自动打开文件,而是出现另存为对话框
- Asp直接下载而不是在浏览器打开函数