熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

Response.WriteFile無法下載大文件

2022-06-13   來源: .NET編程 

當您嘗試使用 responsewritefile 方法下載大文件時下載操作可能沒有響應並且隨後可能會收到以下錯誤信息之一
The page cannot be displayed
Server Application Unavailable

The Web application you are attempting to access on this Web server is currently unavailablePlease hit the Refresh button in your Web browser to retry your request

Administrator Note:An error message detailing the cause of this specific request failure can be found in the system event log of the web serverPlease review this log entry to discover what caused this error to occur您還可能會在應用程序事件日志中看到以下消息

  Aspnet_wpexe(對於在 Microsoft Internet 信息服務 [IIS] 上運行的應用程序則為 Wwpexe)意外停止

在此過程中您還可能會發現 Web 服務器的內存使用量增加
回到頂端

原因


Web 服務器計算機的硬件配置決定您可以成功下載的最大文件大小當 ASPNET 輔助進程(Aspnet_wpexe對於在 Internet 信息服務 [IIS] 上運行的應用程序則為 Wwpexe)執行文件下載請求時會出現文件下載對話框ASPNET 輔助進程開始向 Microsoft Internet 信息服務進程(Inetinfoexe 或 Dllhostexe)發送數據它不等您單擊確定即開始發送

根據計算機的配置IIS 進程可能會處理數據也可能會將數據緩存在內存中如果文件太大在這兩個進程相互通信的過程中數據將被緩存在內存中這可能會導致服務器上的內存使用量增加出現此錯誤的原因是 Web 服務器上的內存限制
回到頂端

替代方法


要解決此問題請使用以下任一方法 將數據分成較小的部分然後將其移動到輸出流以供下載從而獲取這些數據以下代碼演示了如何完成此操作

重要說明當您在 ASPNET 應用程序的 nfig 文件中將編譯元素的 debug 屬性值設置為 false 時必須針對要下載的文件大小將 serverscripttimeout 屬性設置為適當的值默認情況下serverscripttimeout 值被設置為 但是當 debug 屬性被設置為 true 時serverscripttimeout 值將被設置為一個非常大的值( 秒)作為一名開發人員您必須知道這可能會對您的 ASPNET Web 應用程序的行為造成的影響

此外在下面的代碼中您還必須知道與 filestream 構造函數一起使用的參數值指定的枚舉值會對提供的功能產生重大影響有關更多信息請參考 參考 一節中的 filestream 鏈接
visual Basic NET 代碼

  Dim iStream As SystemIOStream Buffer to read K bytes in chunk: Dim buffer() As Byte Length of the file: Dim length As Integer Total bytes to read: Dim dataToRead As Long Identify the file to download including its path Dim filepath As String = DownloadFileName Identify the file name Dim filename As String = SystemIOPathGetFileName(filepath) Try Open the file iStream = New SystemIOFileStream(filepath SystemIOFileModeOpen _ IOFileAccessRead IOFileShareRead) Total bytes to read: dataToRead = iStreamLength ResponseContentType = application/octetstream ResponseAddHeader(ContentDisposition attachment; filename= & filename) Read the bytes While dataToRead > Verify that the client is connected If ResponseIsClientConnected Then Read the data in buffer length = iStreamRead(buffer ) Write the data to the current output stream ResponseOutputStreamWrite(buffer length) Flush the data to the HTML output ResponseFlush() ReDim buffer() Clear the buffer dataToRead = dataToRead length Else prevent infinite loop if user disconnects dataToRead = End If End While Catch ex As Exception Trap the error if any ResponseWrite(Error : & exMessage) Finally If IsNothing(iStream) = False Then Close the file iStreamClose() End If End Try

Visual C# NET 代碼

  SystemIOStream iStream = null; // Buffer to read K bytes in chunk: byte[] buffer = new Byte[]; // Length of the file: int length; // Total bytes to read: long dataToRead; // Identify the file to download including its path string filepath = DownloadFileName; // Identify the file name string filename = SystemIOPathGetFileName(filepath); try { // Open the file iStream = new SystemIOFileStream(filepath SystemIOFileModeOpen SystemIOFileAccessReadSystemIOFileShareRead); // Total bytes to read: dataToRead = iStreamLength; ResponseContentType = application/octetstream; ResponseAddHeader(ContentDisposition attachment; filename= + filename); // Read the bytes while (dataToRead > ) { // Verify that the client is connected if (ResponseIsClientConnected) { // Read the data in buffer length = iStreamRead(buffer ); // Write the data to the current output stream ResponseOutputStreamWrite(buffer length); // Flush the data to the HTML output ResponseFlush(); buffer= new Byte[]; dataToRead = dataToRead length; } else { //prevent infinite loop if user disconnects dataToRead = ; } } } catch (Exception ex) { // Trap the error if any ResponseWrite(Error : + exMessage); } finally { if (iStream != null) { //Close the file iStreamClose(); } }

將 DownloadFileName 替換為大於 MB 的文件的名稱

&#; 為用戶提供用於下載文件的鏈接

&#; 使用 Microsoft ASP 進行下載或者與 ASP 一起使用 Software Artisans FileUp

&#; 創建 ISAPI 擴展以下載文件

&#; 使用 FTP 下載文件
回到頂端

狀態


這種現象是設計導致的
回到頂端

更多信息


重現此問題的步驟

在 Microsoft Visual Basic NET 或 Microsoft Visual C# NET 中新建一個 Web 應用程序項目默認情況下將創建 WebFormaspx 將一個按鈕對象從工具箱拖到 WebFormaspx 雙擊該按鈕對象以便在代碼視圖中打開 click 事件 將以下代碼粘貼到 Button click 事件中

visual Basic NET 代碼

   Identify the file to download including its path Dim filepath As String = DownloadFileName Identify the file name Dim filename As String = SystemIOPathGetFileName(filepath) ResponseClear() Specify the Type of the downloadable file ResponseContentType = application/octetstream Set the Default file name in the FileDownload dialog box ResponseAddHeader(ContentDisposition attachment; filename= & filename & ) ResponseFlush() Download the file ResponseWriteFile(filepath)


Visual C# NET 代碼

  // Identify the file to download including its path string filepath = DownloadFileName; // Identify the file name string filename = SystemIOPathGetFileName(filepath); ResponseClear(); // Specify the Type of the downloadable file ResponseContentType = application/octetstream; // Set the Default file name in the FileDownload dialog box ResponseAddHeader(ContentDisposition attachment; filename= + filename); ResponseFlush(); // Download the file ResponseWriteFile(filepath);

將 DownloadFileName 替換為大於 MB 的文件的名稱 調試菜單上單擊開始 單擊Button
From:http://tw.wingwit.com/Article/program/net/201311/13320.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.