找回密码
 加入怎通
查看: 913|回复: 2

[其他] ASP.NET MVC:经过FileResult向阅读器发送资料

[复制链接]
杨莹莹连 发表于 2013-11-08 11:39:39 | 显示全部楼层 |阅读模式
  正在 Controller 中咱们能够运用 FileResult 向存户端发送资料。7 L7 o, I# \* U6 T

# L5 H1 ?/ a( R  FileResult6 r9 ~% y. e  |7 T

4 H1 p8 f( b; z  FileResult 是一度形象类,承继自 ActionResult。正在 System.Web.Mvc.dll 中,它有如上三个子类,辨别以没有同的形式向存户端发送资料。+ O3 v) ]+ w, O0 c

7 M5 c% D3 ~! f" c$ O2 \  正在实践运用中咱们一般没有需求间接范例化一度 FileResult 的子类,由于 Controller 类曾经需要了六个 File 办法来简化咱们的操作:
0 P* h& I- W! \+ ~& Z
7 ^- G' n' H! A1 c/ H& U4 R  protected internal FilePathResult File(string fileName, string contentType);protected internal virtual FilePathResult File(string fileName, string contentType, string fileDownloadName);protected internal FileContentResult File(byte[] fileContents, string contentType);protected internal virtual FileContentResult File(byte[] fileContents, string contentType, string fileDownloadName);protected internal FileStreamResult File(Stream fileStream, string contentType);protected internal virtual FileStreamResult File(Stream fileStream, string contentType, string fileDownloadName);
( X; S9 v; }# b' A* |1 ~% u& k! H6 v, V% H, [0 v/ k7 ]
  FilePathResult
: g. j# H8 \) H- F3 O. i; ^$ d% b& b7 _2 Q" L1 \* E$ P$ o
  FilePathResult 间接将磁盘上的资料发送至阅读器:% A& F9 T9 |/ j5 t

( ^, q# O- t) ~7 F) C  1. 最容易的形式5 D* P8 T  w& d; r8 M1 F
$ q$ [5 W! n3 E4 p& [
  public ActionResult FilePathDownload1(){ var path = Server.MapPath("~/Files/鹤冲天.zip"); return File(path, "application/x-zip-compressed");
' q. X/ B# k, H% X8 _/ ]& G# Z" k- H' G
  第一度参数指名资料门路,第二个参数指名资料的 MIME 类型。用户点击阅读器上的键入链接后,会调出键入窗口:
1 f9 F/ u3 W" _' b3 P0 }# V$ [7 |1 L: P" Z8 A
  自己该当留意到,资料称号会成为 Download1.zip,默许成了 Action 的名字。咱们运用 File 办法的第二个重载来处理资料名的成绩:$ Y" R% p8 l- G% X0 c7 v* L

$ S# B% q: E) x  2. 指名 fileDownloadName+ m) l  f2 l8 C5 D' W

2 v$ m) g3 t- d2 Q; u9 l2 J  public ActionResult FilePathDownload2(){ var path = Server.MapPath("~/Files/鹤冲天.zip");  return File("g:\\鹤冲天.zip", "application/x-zip-compressed", "crane.zip");}public ActionResult FilePathDownload3(){ var path = Server.MapPath("~/Files/鹤冲天.zip");  var name = Path.GetFileName(path); return File(path, "application/x-zip-compressed", name);}1 C, G6 k2 D4 Y8 r% g

! B( i, O: D) ~7 A  咱们能够经过给 fileDownloadName 参数传值来指名资料名,fileDownloadName + F. d4 q2 F, w8 b
' V3 W& j+ E& e' s
  无须和磁盘上的资料名一样。键入提醒窗口辨别如次:FilePathDownload2 没成绩,FilePathDownload3 还是默以为了 Action 的名字。缘由是
6 N, G) f# S. R% u( h) Z0 q
. O7 p7 _! w! D3 |+ k& b/ m  fileDownloadName 将作为 URL 的一全体,只能蕴含 ASCII 码。咱们把 FilePathDownload3 改良一下:3. 对于 fileDownloadName 停止 Url 补码public ActionResult FilePathDownload4()
* j8 f$ p. B7 ^. y( Z! p+ B  J
; i0 ?$ X- p. P1 B  {
& V6 A' _0 O& ]" {. D. x( G# V  o3 a9 x5 v2 C2 k# b
  var path = Server.MapPath("~/Files/鹤冲天.zip");
% _4 k- K4 `+ c; d, l* V6 ^- _
0 X1 u! _( K7 Z+ S' e5 h  var name = Path.GetFileName(path);4 V3 C1 D3 L1 Q) d$ L

) V4 }; o. h% ]5 I: w  return File(path, "application/x-zip-compressed", Url.Encode(name));8 H& d1 h7 v4 E! r1 q* {9 Y/ P
7 Q0 t4 }. e1 |4 d; a
  }9 h* j. [& Z& `# k, x. _" M, q

# ?4 y0 O" ~" v) {}

暂时无法加载帖子列表

回复

使用道具 举报

FOSTER 发表于 2026-05-21 15:11:48 | 显示全部楼层
蹲了这么久,终于看到有价值的讨论,支持一下!
回复

使用道具 举报

卟顽正 发表于 2026-09-08 04:43:49 | 显示全部楼层
这个分享太实用了,刚好能用到,感谢楼主!
回复

使用道具 举报

    您需要登录后才可以回帖 登录 | 加入怎通

    本版积分规则

    QQ|手机版|小黑屋|网站地图|真牛社区 ( 苏ICP备2023040716号-2 )

    GMT+8, 2026-9-12 15:41 , Processed in 0.053083 second(s), 25 queries , Gzip On.

    免责声明:本站信息来自互联网,本站不对其内容真实性负责,如有侵权等情况请联系420897364#qq.com(把#换成@)删除。

    Powered by Discuz! X3.5

    快速回复 返回顶部 返回列表