找回密码
 加入怎通
查看: 874|回复: 1

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

[复制链接]
杨莹莹连 发表于 2013-11-08 11:39:39 | 显示全部楼层 |阅读模式
  正在 Controller 中咱们能够运用 FileResult 向存户端发送资料。
$ d8 n+ |3 h3 t/ i, H
! Z# S6 [( h/ l$ _" R& u  FileResult0 n+ \# h; ?7 ?# [7 o

! M! f( o1 e( j% F. _1 j  FileResult 是一度形象类,承继自 ActionResult。正在 System.Web.Mvc.dll 中,它有如上三个子类,辨别以没有同的形式向存户端发送资料。- v4 m' T! a9 {+ X- L$ b

5 w) A* n! l0 m7 Q" _$ r9 n  正在实践运用中咱们一般没有需求间接范例化一度 FileResult 的子类,由于 Controller 类曾经需要了六个 File 办法来简化咱们的操作:% \% `% m6 \, ^1 |! M
" J/ q% b0 s' H2 _- [5 V9 Z
  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);
7 \  q& q$ V4 ?% m$ n3 F) I* s9 V1 ^5 Z' ^
  FilePathResult
: G* E) A( z4 R. Z$ c/ N/ ~/ J2 p$ ]; O
  FilePathResult 间接将磁盘上的资料发送至阅读器:( M+ l& d0 z0 Q! N  u0 G: ?* W" Z
+ w2 [1 P$ ~) n. `! X& C
  1. 最容易的形式9 ^: V0 V  X+ J8 y5 e' q
, T1 D/ l, H. h  w) Y* l0 O3 g
  public ActionResult FilePathDownload1(){ var path = Server.MapPath("~/Files/鹤冲天.zip"); return File(path, "application/x-zip-compressed");
" B; _% F% ~+ U, o5 _' v
  o& v' W- L  v7 ?6 Y5 u1 g, v& r  第一度参数指名资料门路,第二个参数指名资料的 MIME 类型。用户点击阅读器上的键入链接后,会调出键入窗口:
, ?& p, S: t4 [5 |( ?; A* j; Q# z! ~6 I8 U% K6 m& P9 d3 e$ G" u
  自己该当留意到,资料称号会成为 Download1.zip,默许成了 Action 的名字。咱们运用 File 办法的第二个重载来处理资料名的成绩:3 j* Y9 t+ A. `3 x
5 m' p& m$ o2 F" S% t
  2. 指名 fileDownloadName; H7 ^: {4 }2 {" d0 r6 n8 P6 U
! x5 t2 t3 j+ b' e2 l
  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);}
3 c' s9 P  b! i) _8 G  K- s  M" \. z( x% W
  咱们能够经过给 fileDownloadName 参数传值来指名资料名,fileDownloadName ( i0 U4 [6 Q; Z0 ^7 j& S
& j& v9 S8 V( P: q" R, K8 _
  无须和磁盘上的资料名一样。键入提醒窗口辨别如次:FilePathDownload2 没成绩,FilePathDownload3 还是默以为了 Action 的名字。缘由是 + v, E5 e# w# Z8 F+ z6 O% i% P, u

; e, T2 K  ?( L3 g: }  fileDownloadName 将作为 URL 的一全体,只能蕴含 ASCII 码。咱们把 FilePathDownload3 改良一下:3. 对于 fileDownloadName 停止 Url 补码public ActionResult FilePathDownload4()
, V  A: ^0 P1 @. A/ N' U% z( ]+ A4 u  @! N
  {
& {6 U, M- C, N" U8 n# q
! \( L* h  o( U: X  var path = Server.MapPath("~/Files/鹤冲天.zip");9 x3 a; G, a( f

# X2 H  a- p3 B6 }. x  var name = Path.GetFileName(path);
) l* R" F3 O" U3 S
1 `! I& }5 x6 s6 j2 Y$ P: y  return File(path, "application/x-zip-compressed", Url.Encode(name));  u+ J1 \/ {# B* m, D' g+ b

. Z- L" g3 ?7 }' y  }
0 C$ u! {! L) s9 x/ ^/ C3 @* F' `7 Y/ s, K8 ^3 L
}
回复

使用道具 举报

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

使用道具 举报

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

    本版积分规则

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

    GMT+8, 2026-6-12 11:11 , Processed in 0.020799 second(s), 22 queries , Gzip On.

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

    Powered by Discuz! X3.5

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