2013-08-28 112 views
0

我正在开发asp.net,c#2010中的项目。在那篇文章中,我尝试以.zip格式下载mp3文件。即使尺寸在80到150 MB之间,它在本地PC上也能正常工作。它也适用于直播服务器,而.zip文件大小介于10 to 20 MB之间,但现在我已经上传了80 to 150 MB之间的文件,它无法正常工作,并且不会提供任何只载入页面的错误。正在加载页面可能是因为我在web.config文件中设置了最大超时时间。下载带有最大文件大小限制的.zip文件

下载代码

if (File.Exists(virtualPath)) 
{ 
    FileInfo file = new FileInfo(virtualPath); 
    Response.Clear(); 
    Response.ClearHeaders(); 
    Response.ClearContent(); 
    Response.ContentType = "application/zip"; 
    Response.AddHeader("content-disposition", "attachment;filename=\"" + Path.GetFileName(AlbumName) + "\""); 
    Response.AppendHeader("content-length", file.Length.ToString()); 
    Response.WriteFile(virtualPath); 
    //Response.End(); 
    HttpContext.Current.ApplicationInstance.CompleteRequest(); 
} 

的Web.config

<system.web> 
     <customErrors mode="Off"></customErrors> 
     <compilation debug="true" targetFramework="4.0" /> 
     <httpRuntime maxRequestLength="2000000000" executionTimeout="999999"/> 
</system.web> 

<system.webServer> 
    <security> 
     <requestFiltering> 
       <requestLimits maxAllowedContentLength="2000000000" /> 
     </requestFiltering> 
    </security> 
    <modules runAllManagedModulesForAllRequests="true"/> 
</system.webServer> 

不知道是什么原因。请帮我解决这个错误。如果你有替代解决方案,那将会很棒。

编辑

if (File.Exists(virtualPath)) 
{ 
    FileInfo file = new FileInfo(virtualPath); 
    Response.Clear(); 
    Response.ClearHeaders(); 
    Response.ClearContent(); 
    Response.ContentType = "application/zip"; 
    Response.AddHeader("content-disposition", "attachment;filename=\"" + Path.GetFileName(AlbumName) + "\""); 
    Response.AppendHeader("content-length", file.Length.ToString()); 
    Response.Buffer = false; 
    Response.TransmitFile(virtualPath); 
    //Response.WriteFile(virtualPath); 
    //Response.End(); 
    HttpContext.Current.ApplicationInstance.CompleteRequest(); 
} 

感谢

+0

是否有IIS相关的错误?查看事件查看器。 – dcaswell

+0

可能是它的IIS相关的错误,但我不知道。我怎样才能使用事件查看器? –

+0

从开始菜单:事件查看器 – dcaswell

回答

2

如果使用Response.TransmitFile(Server.MapPath(virtualpath)),它会关闭缓冲,你不必写入输出流。

+0

谢谢ps2goat:它适合我! –

+0

嗨ps2goat:我越来越'远程主机关闭连接。错误代码是0x80072746.'异常使用'Response.Buffer = false; Response.TransmitFile(virtualPath);'并且移除了'Response.WriteFile(virtualPath);'。你能帮我解决这个问题吗? –

+0

添加日志记录 - 启动下载和发生错误需要多长时间?会议过期了吗?例如,您是否退出了长时间与网站互动,比会话超时长? – ps2goat

0

尝试WriteFile的前加入这一行:

Response.Buffer = false; 
+0

我出现如下错误:**连接已重置** 与服务器的连接已重置而页面正在加载。 –

+0

这个陈述需要先来。 – richardtallent

+0

嗨richardtallent:看我的编辑代码。我现在使用这个,但我得到这个错误。 '远程主机关闭了连接。错误代码是0x80072746.'在哪里我必须把'Response.Buffer = false;'这个语句? –

0

有一个叫IIS设置:

maxAllowedContentLength

可选UINT属性。

指定请求中内容的最大长度(以字节为单位)。

的默认值是30000000

猜你必须改变你的IIS设置,允许超过30MB的文件。

在webconfig中,maxRequestLength的单位是kB!

打开文件 C:\ WINDOWS \ SYSTEM32 \ INETSRV \设置\的applicationHost.config并找到 行:

设置overrideModeDefault属性设置为允许。所以,现在该行应 样子:

<section name="requestFiltering" overrideModeDefault="Allow" />

+0

我已经在web.config中设置了'maxRequestLength'。你可以看到我的问题。我补充说。 –

+0

检查是否设置了overrideModeDefault =“允许” –

+1

感谢Nikolaj Zander,因为它的'overrideModeDefault =“Allow”'。通过使用'Response.TransmitFile(virtualPath)'文件而不是'Response.WriteFile(virtualPath)'解决任何方式问题。感谢您宝贵的回复。 –