直接下載一個檔案

來源:互聯網
上載者:User

是一個內容管理系統,由於使用權限設定的原因,堅決不允許將文檔或者程式存放在一個IIS無法訪問的目錄,以免造成泄密事件。因此要採用利用Asp.Net讀出檔案內容後回寫到瀏覽器中的方法,看了uestc95 的[.NET Tips 1001]ASP.NET直接下載一個檔案,而不是在IE中開啟它 後很有啟發,不過偷懶的天性是我覺得這個方法有點麻煩。好在發現Response.Writefile 這個方法,可以最大限度的減少同志們的工作量。,代碼如下  

        Response.Clear()

        Response.ContentType = "application/octet-stream"

        Response.AddHeader("Content-Disposition", "attachment;FileName=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8))

        Response.WriteFile(filepath+filname)

        Response.End()

當然大家可以在這裡添加一些許可權判斷的代碼。

http://support.microsoft.com/default.aspx?scid=kb;EN-US;317719
How To Export Data in a DataGrid on an ASP . NET WebForm to Microsoft Excel

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.