在页面加载事件中下载文件?

时间:2011-08-29 03:51:39

标签: asp.net

如何在页面加载事件中下载文件[不点击任何按钮或链接或锚标记],我想在页面中下载文件加载自己,请帮助我,谢谢

2 个答案:

答案 0 :(得分:1)

Page_Load方法中添加以下行,设置相应的内容类型,并将yourfile.extention替换为您要下载的文件的全名。

this.context.Response.ContentType = "application/filetype";
this.context.Response.AddHeader("content-disposition", "attachment;filename=yourfile.extension");

答案 1 :(得分:1)

假设您要将文件下载到磁盘上并具有该路径。以下代码可以解决问题。

// You should put more appropriate MIME type as per your file time - perhaps based on extension
Response.ContentType = "application/octate-stream"; 
Response.AddHeader("content-disposition", "attachment;filename=[your file name w/o path]");
// Start pushing file to user, IIS will do the streaming.
Response.TransmitFile(filePath);