ASP.NET 附件彈窗下載

來源:互聯網
上載者:User

      發的隨筆,不能每個都上首頁,微微蛋疼啊。啊哈。這次再發一個關於附件下載的。

      前段時間做項目,本來附件下載直接一個a標記找到檔案就可以了。但是呢,每次開啟txt啊,圖片啊。都是直接瀏覽器開啟。我們測試都說了,你們那咋搞的,我一點就開啟了。想下載也沒辦法(搞了個自訂右鍵菜單),我說那是你系統問題。關我電腦毛事啊。。人說,你看別人的都可以。。。然後我就傷不起了。。就有了這麼一小段代碼。

      話說,測試跟開發真是天生冤家啊。好好的代碼,本地一點事沒有,一到測試環境,各種奇葩BUG,每次都先跟他們說,清緩衝,清緩衝的。。。我恨測試。

      其實很簡單的代碼,就是加一個全域Hanlder。

AttachmentDownloadHanlder

 1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Text; 6 using System.Web; 7  8 namespace Wanda.SICI.UI.Web 9 {10     public class AttachmentDownloadHanlder : IHttpHandler11     {12         public void ProcessRequest(HttpContext context)13         {14             string path = context.Request.PhysicalPath;15 16             try17             {18                 context.Response.Charset = "UTF-8";19                 context.Response.ContentEncoding = Encoding.UTF8;20                 context.Response.HeaderEncoding = Encoding.UTF8;21                 context.Response.AppendHeader("Content-disposition",22                                               "attachment; filename=" +23                                               HttpUtility.UrlEncode(Path.GetFileName(path), Encoding.UTF8));24                 context.Response.WriteFile(path);25             }26             catch (Exception ex)27             {28                 context.Response.Write("下載檔案出錯,原因:" + ex.Message);29             }30             finally31             {32                 context.Response.End();33             }34         }35 36         public bool IsReusable { get { return false; } }37     }38 }

 

然後再添加一個設定檔,設定檔放到你的附件根目錄就可以了。

 

web.config

 1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3   <system.web> 4     <httpHandlers> 5       <add verb="*" path="*" type="Wanda.SICI.UI.Web.AttachmentDownloadHanlder"/> 6     </httpHandlers> 7   </system.web> 8   <system.webServer> 9     <handlers>10       <add name="AttachmentDownloadHanlder" verb="*" path="*"11         modules="IsapiModule" 12         scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll"13         resourceType="File"14            />15     </handlers>16   </system.webServer>17 </configuration>

如果報錯的話,就把%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll改成

%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll 就可以了。

最開始的時候,一直500,後來一查,說什麼要啟用iis的32位類比什麼的。

這樣每次下載,都會把附件標記,不會使用瀏覽器開啟了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.