ASP.NET (C#) 强制文件下载
2011-07-22 16:28
190 查看
Today'sBrowsersarecapableofopeningmanydifferenttypesoffiles.Forexample,ifyouclickonalinktoMP3file,mostBrowserswillplayitwithintheBrowser.Butsometimesyoumaywanttoprovidea'DownloadFile'link,whichwillforcetheBrowsertosavethetargetfileonuser'scomputereveniftheBrowseriscapableofopeningit.RecentlyIworkedonaWebsitethatofferedadownloadoptiontomusicfiles.
InadditiontoforcingdownloadofaFile,youmayalsowanttoperformtaskslikeverifyingiftheuserhasaccesstotheFile,orkeepacountofnumberoftimesaFilehasbeendownloaded.
ThesimpletricktoforcedownloadofaFileisaddingaHTTPheadercalledcontent-disposition=attachment.YoucanalsoaddafilenametothisheadertosuggestaFileNamefortheDownloadedFile.
TheexampleaboveisanExtensionmethodforHttpResponse,andwillworkforASP.NET3.5onwards.IfyouaddthisclasstoApp_Code,anewmethodnamed'ForceDownload()'willbeavailableontheResponseobject.HereisanexampleyoucanuseanywhereinanASP.NETPage(Page_LoadEvent,forexample):
CallingthismethodwillendtheresponseaftersendingtheFiletoclient.
ForASP.NET2.0,youcanusethebodyoftheExtensionmethodandaddittoPage_LoadEventdirectly.
MyobservationisthatifthereisnoContentTypepresent,IEusesthefileextensiontouderstandthecontent.
InadditiontoforcingdownloadofaFile,youmayalsowanttoperformtaskslikeverifyingiftheuserhasaccesstotheFile,orkeepacountofnumberoftimesaFilehasbeendownloaded.
ThesimpletricktoforcedownloadofaFileisaddingaHTTPheadercalledcontent-disposition=attachment.YoucanalsoaddafilenametothisheadertosuggestaFileNamefortheDownloadedFile.
HereisanASP.NETexamplecode:
01 | /*********** |
02 | Author:AbuHaider(http://www.haiders.net) |
03 | Nov2008 |
04 | AddsExtensionMethod"ForceDownload"toHttpResponseclass |
05 | Requires:ASP.NET3.5 |
06 | DropthisinApp_Code |
07 | /************/ |
08 |
09 |
10 | public static class HttpExtensions |
11 | { |
12 |
13 | //ForcesDownload/SaveratherthanopeninginBrowser// |
14 | public static void ForceDownload( this HttpResponseResponse, string virtualPath, string fileName) |
15 | { |
16 |
17 | Response.Clear(); |
18 | Response.AddHeader( "content-disposition" , "attachment;filename=" +fileName); |
19 | Response.WriteFile(virtualPath); |
20 | Response.ContentType= "" ; |
21 | Response.End(); |
22 |
23 | } |
24 |
25 | } |
1 | Response.ForceDownload( "~/Downloads/MyMusic.mp3" , "MyMusic.mp3" ); |
ForASP.NET2.0,youcanusethebodyoftheExtensionmethodandaddittoPage_LoadEventdirectly.
AboutContentType
TheaboveExtensionMethodsimplyclearstheCotnentTypeheader.Thisistokeepthemethodgeneric.IfyouwouldliketospecifyContentTypeforthetargetFile,youcanaddanotherParametertothemethodandassignittotheContenTypeheaderinsteadofclearingit.ContentTypeheaderisusedbytheBrowsertounderstandwhattypeofdataisbeingreceivedsotheycandisplayitproperlyoruseanassociatedprogramtoopenit.ClearingtheContentTypedoesworkinthiscasesincewedonotwanttheBrowsertotryandopentheFilewithanassociatedprogram,butsimplysaveittodisk.MyobservationisthatifthereisnoContentTypepresent,IEusesthefileextensiontouderstandthecontent.
相关文章推荐
- ASP.NET C# 文件下载速度限制
- C# asp.net中四种文件下载方法
- asp.net(C#)上传下载及文件管理
- asp.net C#实现下载文件的六种方法实例
- asp.net(C#)上传下载及文件管理代码(续)
- ASP.NET(C#) Web Api通过文件流下载文件到本地实例
- Asp.net 2.0 用C# 创建 PDF文件[引用] (示例代码下载)
- ASP.net 实现文件强制下载
- asp.net(C#)上传下载及文件管理
- C#ASP.NET中四种文件下载方法
- Asp.net 2.0 用C# 创建 PDF文件[引用] (示例代码下载)
- asp.net C#实现下载文件的六种方法实例
- ASP.NET(c#)文件下载函数
- Asp.net 2.0 用C# 创建 PDF文件[引用] (示例代码下载)
- c# ASP.NET 下载文件到本地
- 用C#(ASP.NET)怎么实现文件下载功能
- asp.net(C#)上传下载及文件管理
- Asp.net 2.0 用C# 创建 PDF文件[引用] (示例代码下载)
- C#ASP.NET中四种文件下载方法
- asp.net(C#)上传下载及文件管理代码