找回密码
 加入怎通
查看: 866|回复: 0

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

[复制链接]
杨莹莹连 发表于 2013-11-08 11:39:39 | 显示全部楼层 |阅读模式
  正在 Controller 中咱们能够运用 FileResult 向存户端发送资料。
) W" x: Z' h# `# a1 x
! d+ n- q2 K8 a) P! |9 Z2 ]  FileResult
6 ~" W2 p" I- Q) C# a8 {7 U/ F+ p4 h7 d! F& m: ~0 M5 T0 K
  FileResult 是一度形象类,承继自 ActionResult。正在 System.Web.Mvc.dll 中,它有如上三个子类,辨别以没有同的形式向存户端发送资料。
7 h( o! \6 k5 O: g% c
3 T% J3 |5 n  r" N: z; s  正在实践运用中咱们一般没有需求间接范例化一度 FileResult 的子类,由于 Controller 类曾经需要了六个 File 办法来简化咱们的操作:
1 W0 h# V6 H# j0 b1 q" F" a
# {# x/ B/ ^; ~  d) v% T  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);
) c9 |# X/ A7 R0 \" S! R9 T
5 H1 y" S5 s" m: r3 b  FilePathResult( B0 i8 _3 v8 n5 C9 B
0 t# L+ W* n: e$ B! |6 f* }
  FilePathResult 间接将磁盘上的资料发送至阅读器:7 U$ e# N+ |$ i# C$ B# x8 }4 L

) b. _2 l: ~' M! d# {4 I9 Y  1. 最容易的形式& K& n- e( G2 p2 B8 W5 Z% O0 A

, A3 E# Y6 C, B7 U+ B' X  public ActionResult FilePathDownload1(){ var path = Server.MapPath("~/Files/鹤冲天.zip"); return File(path, "application/x-zip-compressed");2 Z1 c/ \  T9 Y  Y

) |- s5 Y. ], }4 J# u! i  第一度参数指名资料门路,第二个参数指名资料的 MIME 类型。用户点击阅读器上的键入链接后,会调出键入窗口:
# U! R& B# u. x9 g* k2 W9 b! R! H4 E# S
  自己该当留意到,资料称号会成为 Download1.zip,默许成了 Action 的名字。咱们运用 File 办法的第二个重载来处理资料名的成绩:1 w8 N* p. \% X  r; Q+ Y+ s1 w# w0 V/ S
* s" S0 ]( U* z' H
  2. 指名 fileDownloadName  E2 F+ P# Z8 M# U% X  b2 i

: |7 e( W! g/ C5 v( I$ ?  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);}: M6 U% N/ _% i3 B
3 i- x5 B3 h) @  B  ]0 y
  咱们能够经过给 fileDownloadName 参数传值来指名资料名,fileDownloadName , m) h4 [0 Z0 Y7 J

  ]. Z" R; a4 x- y$ _; E  V+ F3 O  无须和磁盘上的资料名一样。键入提醒窗口辨别如次:FilePathDownload2 没成绩,FilePathDownload3 还是默以为了 Action 的名字。缘由是
; n5 b  a5 D  y9 K
8 k7 |! B7 q5 M$ U5 c. V+ Q  fileDownloadName 将作为 URL 的一全体,只能蕴含 ASCII 码。咱们把 FilePathDownload3 改良一下:3. 对于 fileDownloadName 停止 Url 补码public ActionResult FilePathDownload4()
' Z  a" I$ G% V% V5 W  e* e3 j% b/ B$ m
  {
! L3 S+ u. N8 V' |- H$ T( A1 h- L- H& E9 O8 u  i- n: i4 P7 K4 f
  var path = Server.MapPath("~/Files/鹤冲天.zip");- _  J; g( q5 I; r; ]

# I3 R& G' O. b) s  var name = Path.GetFileName(path);  m/ b) u( H' ^" y7 n% D2 s

% Y% \" e# J& Y4 {0 z. s' C  return File(path, "application/x-zip-compressed", Url.Encode(name));4 H' R! q# b1 ^$ l

, t2 }; M3 R) B  B5 _% I9 e0 _  }
1 n( t' s5 k! N) z) j+ D
& {' U7 {+ v# a7 B1 {}
回复

使用道具 举报

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

    本版积分规则

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

    GMT+8, 2026-4-28 07:03 , Processed in 0.249048 second(s), 23 queries , Gzip On.

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

    Powered by Discuz! X3.5

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