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

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

[复制链接]
杨莹莹连 发表于 2013-11-08 11:39:39 | 显示全部楼层 |阅读模式
  正在 Controller 中咱们能够运用 FileResult 向存户端发送资料。2 Y5 |1 N) \9 P- D; r+ y  m7 C  G3 z
) N6 b% o' V4 }/ Z  f
  FileResult
4 M" q! A7 T* h2 G) [9 N; E( E1 d* o. p3 R7 r  `3 r* x
  FileResult 是一度形象类,承继自 ActionResult。正在 System.Web.Mvc.dll 中,它有如上三个子类,辨别以没有同的形式向存户端发送资料。
* f9 z. {; w8 l& o
5 H. Y# d7 m4 K" |" }, y  正在实践运用中咱们一般没有需求间接范例化一度 FileResult 的子类,由于 Controller 类曾经需要了六个 File 办法来简化咱们的操作:
: K; C6 Y2 _0 O
) H. a* @# t5 w- H  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);; R( w; t% J) I/ T

2 y$ f7 ~+ `% d0 }" T7 [) Q& Q  FilePathResult
6 \, E4 c4 f* f1 f! E7 {9 W$ Y, Q+ I6 M: G" z* m& _
  FilePathResult 间接将磁盘上的资料发送至阅读器:
$ h- y- a3 @# c. U
$ y0 T# w5 |* E7 |' g  T, }+ g& _  1. 最容易的形式0 t$ @" W' M/ Y2 o

+ q% l+ ^. m# n) V9 U  public ActionResult FilePathDownload1(){ var path = Server.MapPath("~/Files/鹤冲天.zip"); return File(path, "application/x-zip-compressed");( K# J3 o* ]; ^& Z$ ]% A( T! M

7 q# a, e/ p! u+ T2 [! m  第一度参数指名资料门路,第二个参数指名资料的 MIME 类型。用户点击阅读器上的键入链接后,会调出键入窗口:& k9 _& _+ \! U# y  H7 T

1 _* k  V8 v2 v4 ~2 x5 t3 y6 _  自己该当留意到,资料称号会成为 Download1.zip,默许成了 Action 的名字。咱们运用 File 办法的第二个重载来处理资料名的成绩:& ~9 r( X' @$ h: L) [/ w
3 \% G) ^/ L  V+ j
  2. 指名 fileDownloadName
0 }3 C; p% o! a5 m" m5 o6 C7 s+ }9 @& \$ k+ a& `( `* R
  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);}7 e: f0 a) n+ E2 B) O1 f

# g6 A' ^7 }$ v- [& k+ w( ^  咱们能够经过给 fileDownloadName 参数传值来指名资料名,fileDownloadName
7 m  z; U2 d. c' b' @
9 a, _; q5 l3 H  K  无须和磁盘上的资料名一样。键入提醒窗口辨别如次:FilePathDownload2 没成绩,FilePathDownload3 还是默以为了 Action 的名字。缘由是
2 b  W7 j1 a- g% Y+ K! \- z& m8 c3 Q4 f, o9 @1 V$ E
  fileDownloadName 将作为 URL 的一全体,只能蕴含 ASCII 码。咱们把 FilePathDownload3 改良一下:3. 对于 fileDownloadName 停止 Url 补码public ActionResult FilePathDownload4()0 T8 s, `1 W0 G4 ?
7 P4 M, c1 G. [9 g/ r
  {8 [7 E) h' a4 [+ I  X; |
9 b6 M& I" a- j  V5 ?
  var path = Server.MapPath("~/Files/鹤冲天.zip");% N5 [. j/ B* B. T6 s

8 b: M/ o( z3 W% [" m/ a  var name = Path.GetFileName(path);. t3 `- K' s. F: j5 k
5 \4 A& b! }% w9 o
  return File(path, "application/x-zip-compressed", Url.Encode(name));5 W1 v: N% K2 X* H- |4 b- u

  d  Z7 F, C& L/ i  }2 X4 _( w9 ?6 M0 z+ d8 f% S

* ]' E: ~# `5 n9 X- P) ^% e}
回复

使用道具 举报

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

    本版积分规则

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

    GMT+8, 2026-3-13 13:45 , Processed in 0.151442 second(s), 23 queries , Gzip On.

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

    Powered by Discuz! X3.5

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