ASP.MVC下载文件,不需要本地生成文件的方法
今天记录一个不需要在本地生成下载文件的,一种文件下载方式。使用asp.mvc。方式如下
1.在一个controller中,添加如下方法
public class NewsController : Controller { // [HttpPost] 千万不能用post,这样前台使用a标签无法跳转 public FileResult DownFile() { string content = "1111";//生成内容 Response.Clear(); Response.Buffer = false; Response.ContentType = "application/octet-stream"; Response.AppendHeader("content-disposition", "attachment;filename=" + DateTime.Now.ToString("yyyy-MM-dd") + ".xml;"); Response.Write(content); Response.Flush(); Response.End(); return null; } }
这里的DownFile就是下载的方法。返回值为FileResult
2. 在View视图文件中,添加一个连接<a href="/News/DownFile">下载</a>
完成
这里注意,DownFile方法上,一定不能添加httppost的属性,否则超链接将失效

浙公网安备 33010602011771号