本文共 2266 字,大约阅读时间需要 7 分钟。
表示一个用于将二进制文件内容发送到响应的基类。它有三个子类:
FileContentResult
FilePathResultFileStreamResult推荐阅读:https://www.cnblogs.com/weiweixiang/p/5667355.html
首先、创建一个mvc5项目、然后添加一个FileTest控制器,添加以下方法
public ActionResult Export() { // Response.ContentType指定文件类型 可以为application/ms-excel || application/ms-word || application/ms-txt || application/ms-html return File(Server.MapPath("/UserData/test.docx"), "application/ms-word", "test.docx"); }
使用非常方便,这样即可实现下载
public ActionResult Getbg() { string bgimg = AppDomain.CurrentDomain.BaseDirectory + "/UserData/bg.jpg"; Image img = Image.FromFile(bgimg); byte[] bytes = ImageToBytes(img); return File(bytes, "image/jpeg"); }
使用非常方便,这样即可实现图片的显示,在临时描绘图片并展示的场景中非常实用。
public ActionResult ExportDoc() { var path = Server.MapPath("/UserData/test.docx"); var fileName = HttpUtility.UrlEncode("test.docx", Encoding.GetEncoding("UTF-8")); return File(new FileStream(path, FileMode.Open), "application/ms-word", fileName); }
jquery并不支持流文件,
js重要实现来自于情郎的博文:
知识点:
http://www.jb51.net/article/30565.htm
header中Content-Disposition的作用与使用方法:
当代码里面使用Content-Disposition来确保浏览器弹出下载对话框的时候。 response.addHeader("Content-Disposition","attachment");一定要确保没有做过关于禁止浏览器缓存的操作。如下:
response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "No-cache"); response.setDateHeader("Expires", 0);
http://blog.csdn.net/iamiwangbo/article/details/52911716
使用blob实现文件的下载和上传
本文源码下载:https://gitee.com/zmsofts/XinCunShanNianDaiMa/blob/master/ActionResultOfMvc5Study.rar
参考:
https://msdn.microsoft.com/zh-cn/library/system.web.mvc.fileresult.aspx
http://www.cnblogs.com/bmib/p/3518486.html
http://www.runoob.com/ajax/ajax-intro.html ajax学习
https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest
http://www.ruanyifeng.com/blog/2012/09/xmlhttprequest_level_2.html 讲解XMLHttpRequest 1 和2 的区别
http://www.cnblogs.com/cdemo/p/5225848.html
https://www.cnblogs.com/weiweixiang/p/5667355.html
https://www.cnblogs.com/xielong/p/5940535.html