当您尝试使用 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 pathstring filepath = DownloadFileName;// Identify the file namestring filename = SystemIOPathGetFileName(filepath);try{// Open the fileiStream = 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 connectedif (ResponseIsClientConnected) {// Read the data in bufferlength = iStreamRead(buffer );// Write the data to the current output streamResponseOutputStreamWrite(buffer length);// Flush the data to the HTML outputResponseFlush();buffer= new Byte[];dataToRead = dataToRead length;}else{//prevent infinite loop if user disconnectsdataToRead = ;}}}catch (Exception ex) {// Trap the error if anyResponseWrite(Error : + exMessage);}finally{if (iStream != null) {//Close the fileiStreamClose();}} 将 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 pathDim filepath As String = DownloadFileName Identify the file nameDim filename As String = SystemIOPathGetFileName(filepath)ResponseClear() Specify the Type of the downloadable fileResponseContentType = application/octetstream Set the Default file name in the FileDownload dialog boxResponseAddHeader(ContentDisposition attachment; filename= & filename & )ResponseFlush() Download the fileResponseWriteFile(filepath) Visual C# NET 代码 // Identify the file to download including its pathstring filepath = DownloadFileName;// Identify the file namestring filename = SystemIOPathGetFileName(filepath);ResponseClear();// Specify the Type of the downloadable fileResponseContentType = application/octetstream;// Set the Default file name in the FileDownload dialog boxResponseAddHeader(ContentDisposition attachment; filename= + filename);ResponseFlush();// Download the fileResponseWriteFile(filepath); 将 DownloadFileName 替换为大于 MB 的文件的名称在调试菜单上单击开始单击Button |