文谷首页 | 业界传真 | 网络技术 | 服务器 | 数据库 | 存储技术 | 系统安全 | 无线技术 | Cisco | .Net | Windows | Linux | Unix | Java
电子商务 | 网站工程 | 网页设计 | 平面设计 | 多媒体 | 编程语言 | Oracle | MSSQL | Photoshop | ASP | PHP | 实用技巧 | 进程查询 | 文谷论坛
 websphere   .net framework
您现在的位置: IT文谷 >> 开发平台 >> .net framework >> ASP.NET >> 文章正文
Response.WriteFile无法下载大文件2006-7-15Response.WriteFile无法下载大文件2006-7-15Response.WriteFile无法下载大文件
Response.WriteFile无法下载大文件2006-7-15Response.WriteFile无法下载大文件2006-7-15Response.WriteFile无法下载大文件
Response.WriteFile无法下载大文件

Response.WriteFile无法下载大文件2006-7-15Response.WriteFile无法下载大文件2006-7-15Response.WriteFile无法下载大文件
当您尝试使用 response.writefile 方法下载大文件时,下载操作可能没有响应,并且随后可能会收到以下错误信息之一: The page cannot be displayed- 或 - Server Application Unavailable

The Web application you are attempting to access on this Web server is currently unavailable.Please 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 server.Please review this log entry to discover what caused this error to occur.您还可能会在应用程序事件日志中看到以下消息:

Aspnet_wp.exe(对于在 Microsoft Internet 信息服务 [IIS] 6.0 上运行的应用程序,则为 W3wp.exe)意外停止。

在此过程中,您还可能会发现 Web 服务器的内存使用量增加。
回到顶端

原因

Web 服务器计算机的硬件配置决定您可以成功下载的最大文件大小。当 ASP.NET 辅助进程(Aspnet_wp.exe,对于在 Internet 信息服务 6.0 [IIS] 上运行的应用程序,则为 W3wp.exe)执行文件下载请求时,会出现文件下载对话框。ASP.NET 辅助进程开始向 Microsoft Internet 信息服务进程(Inetinfo.exe 或 Dllhost.exe)发送数据。它不等您单击“确定”即开始发送。

根据计算机的配置,IIS 进程可能会处理数据,也可能会将数据缓存在内存中。如果文件太大,在这两个进程相互通信的过程中,数据将被缓存在内存中。这可能会导致服务器上的内存使用量增加。出现此错误的原因是 Web 服务器上的内存限制。
回到顶端

替代方法

要解决此问题,请使用以下任一方法: 将数据分成较小的部分,然后将其移动到输出流以供下载,从而获取这些数据。以下代码演示了如何完成此操作。

重要说明:当您在 ASP.NET 应用程序的 Web.config 文件中将编译元素的 debug 属性值设置为 false 时,必须针对要下载的文件大小将 server.scripttimeout 属性设置为适当的值。默认情况下,server.scripttimeout 值被设置为 90 秒。但是,当 debug 属性被设置为 true 时,server.scripttimeout 值将被设置为一个非常大的值(30,000,000 秒)。作为一名开发人员,您必须知道这可能会对您的 ASP.NET Web 应用程序的行为造成的影响。

此外,在下面的代码中,您还必须知道与 filestream 构造函数一起使用的参数值。指定的枚举值会对提供的功能产生重大影响。有关更多信息,请参考 参考 一节中的 filestream 链接。
visual Basic .NET 代码
      Dim iStream As System.IO.Stream



      ' Buffer to read 10K bytes in chunk:

      Dim buffer(10000) 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 = System.IO.Path.GetFileName(filepath)



      Try

         ' Open the file.

         iStream = New System.IO.FileStream(filepath, System.IO.FileMode.Open, _

                                                IO.FileAccess.Read, IO.FileShare.Read)



         ' Total bytes to read:

         dataToRead = iStream.Length

         

         Response.ContentType = "application/octet-stream"

         Response.AddHeader("Content-Disposition", "attachment; filename=" & filename)



         ' Read the bytes.

            While dataToRead > 0

                ' Verify that the client is connected.

                If Response.IsClientConnected Then

                    ' Read the data in buffer

                    length = iStream.Read(buffer, 0, 10000)



                    ' Write the data to the current output stream.

                    Response.OutputStream.Write(buffer, 0, length)



                    ' Flush the data to the HTML output.

                    Response.Flush()



                    ReDim buffer(10000) ' Clear the buffer

                    dataToRead = dataToRead - length

                Else

                    'prevent infinite loop if user disconnects

                    dataToRead = -1

                End If

            End While



      Catch ex As Exception

         ' Trap the error, if any.

         Response.Write("Error : " & ex.Message)

      Finally

         If IsNothing(iStream) = False Then

            ' Close the file.

            iStream.Close()

         End If

      End Try

Visual C# .NET 代码
System.IO.Stream iStream = null;



// Buffer to read 10K bytes in chunk:

byte[] buffer = new Byte[10000];



// 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  = System.IO.Path.GetFileName(filepath);



try

{

// Open the file.

iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, 

System.IO.FileAccess.Read,System.IO.FileShare.Read);





// Total bytes to read:

dataToRead = iStream.Length;



Response.ContentType = "application/octet-stream";

Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);



// Read the bytes.

  while (dataToRead > 0)

{

// Verify that the client is connected.

if (Response.IsClientConnected) 

{

// Read the data in buffer.

length = iStream.Read(buffer, 0, 10000);



// Write the data to the current output stream.

Response.OutputStream.Write(buffer, 0, length);



// Flush the data to the HTML output.

Response.Flush();



buffer= new Byte[10000];

dataToRead = dataToRead - length;

}

else

{

//prevent infinite loop if user disconnects

dataToRead = -1;

}

}

}

catch (Exception ex) 

{

// Trap the error, if any.

Response.Write("Error : " + ex.Message);

}

finally

{

if (iStream != null) 

{

//Close the file.

iStream.Close();

}

}

将 DownloadFileName 替换为大于 100 MB 的文件的名称。

- 或 -
为用户提供用于下载文件的链接。

- 或 -
使用 Microsoft ASP 3.0 进行下载或者与 ASP 一起使用 Software Artisans FileUp。

- 或 -
创建 ISAPI 扩展以下载文件。

- 或 -
使用 FTP 下载文件。
回到顶端

状态

这种现象是设计导致的。
回到顶端

更多信息

重现此问题的步骤

1. 在 Microsoft Visual Basic .NET 或 Microsoft Visual C# .NET 中,新建一个 Web 应用程序项目。默认情况下,将创建 WebForm1.aspx。
2. 将一个按钮对象从工具箱拖到 WebForm1.aspx。
3. 双击该按钮对象以便在代码视图中打开 click 事件。
4. 将以下代码粘贴到 Button1 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 = System.IO.Path.GetFileName(filepath)



Response.Clear()



' Specify the Type of the downloadable file.

Response.ContentType = "application/octet-stream"



' Set the Default file name in the FileDownload dialog box.

Response.AddHeader("Content-Disposition", "attachment; filename=""" & filename & """")



Response.Flush()



' Download the file.

Response.WriteFile(filepath)

Visual C# .NET 代码
// Identify the file to download including its path.

string filepath = DownloadFileName;



// Identify the file name.

string filename = System.IO.Path.GetFileName(filepath);



Response.Clear();



// Specify the Type of the downloadable file.

Response.ContentType = "application/octet-stream";



// Set the Default file name in the FileDownload dialog box.

Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);



Response.Flush();



// Download the file.

Response.WriteFile(filepath);
5. 将 DownloadFileName 替换为大于 100 MB 的文件的名称。
6. 在“调试”菜单上,单击“开始”。
7. 单击“Button1”

Response.WriteFile无法下载大文件2006-7-15Response.WriteFile无法下载大文件2006-7-15Response.WriteFile无法下载大文件


  • 上一篇文章:

  • 下一篇文章:
  • 进入论坛讨论

    相关文章
    基于.NET的多线程编程入门
    Microsoft .NET 框架资源基础
    VS2005常用插件搜罗
    .net中用GDI+提高gif图片保存画质
    VS.NET:通过Web服务瞄准电子商务
    对象设计中创建VS使用
    安装/初始化/个性化DotNetNuke
    介绍dotnet原动力(DNN)
    GDI+编程10个基本技巧
    DTE .NET工程的自动化编译
    .Net中如何操作IIS的原理分析
    VS.NETRC5无法创建Add-IN工程
    热门文章最新推荐

    版权与免责声明:
    ① 本网转载其他媒体稿件是为传播更多的信息,此类稿件不代表本网观点,版权归原作者所有,本网不承担此类稿件侵权行为的连带责任。
    ② 在本网BBS上发表言论者,文责自负。
    ③ 如您因版权等问题需要与本网联络,请在30日内联系 。
    Response.WriteFile无法下载大文件2006-7-15Response.WriteFile无法下载大文件2006-7-15Response.WriteFile无法下载大文件
    Response.WriteFile无法下载大文件2006-7-15Response.WriteFile无法下载大文件2006-7-15Response.WriteFile无法下载大文件

    .net framework专题
    人气排行
  • 此栏目下没有文章
  • 最近更新
    普通文章技术介绍 新数据网格简介
    普通文章.NET 开发人员该下载的十个必备
    普通文章深入剖析Asp.net资源文件
    普通文章通过事例学习.net的WebForms技
    普通文章ASP.NET Tips1---合并多个字段
    普通文章微软明年1月份出台.NET专业开发
    普通文章微软下月底前完成Vista大部分功
    普通文章揭开微软 .NET 认证考试的面纱
    普通文章Visual Studio:Microsoft开发工
    普通文章Visual Studio:Microsoft开发工
    全站热点       
    最新推荐
    关于文谷 | 联系文谷 | 免责声明 | 文谷社区
    Tel: 0577-65690019    E-mail: ichenjian@gmail.com    MSN:ichenjian@hotmail.com    QQ:2911194
    Copyright © 2004-2006 wengu.org 文谷 All Rights Reserved
    浙ICP备05000327号