现在的位置: 首页 > 综合 > 正文

用Response.BinaryWrite这种方法在下载大于4MB的文件的时候浏览器报500错误的解决方法

2012年12月05日 ⁄ 综合 ⁄ 共 2208字 ⁄ 字号 评论关闭
 

asp中有一种下载方法,代码如下图:

 

call downloadFile(Request("path"))

function downloadFile(strFile)
    strFilename 
= server.MapPath(strFile)    
    
on error resume next
    
      Response.buffer
=True   
    
      
Set   S=CreateObject("Adodb.Stream")     
      S.Mode
=3     
      S.Type
=1     
      S.Open     
      
    
Set fso = Server.CreateObject("Scripting.FileSystemObject")
    
if not fso.FileExists(strFilename) then
        Response.Write(
"<h1>Error:</h1>" & strFilename & " does not exist<p>")
        Response.End
    
end if
    
    
    
Set f = fso.GetFile(strFilename)

    S.LoadFromFile(strFilename)       
 
      if err then
        Response.Write(
"<h1>Error: </h1>" & err.Description & "<p>")
        Response.End
      
else   
              Range
=Mid(Request.ServerVariables("HTTP_RANGE"),7)   
    
              Response.AddHeader   
"Content-Disposition","attachment;   filename="   &   f.name   
    
              
if   Range=""   then   
                    Response.AddHeader   
"Content-Length",   f.size   
             
else   
                    Response.AddHeader   
"Content-Length",   (f.size-Clng(Split(Range,"-")(0)))   
              
End   if   
    
              Response.ContentType
="application/download_only"   
              Response.CharSet   
=   "UTF-8"   
    
              
if   Range=""   then   
                Response.BinaryWrite(S.Read)   
              
else   
                f.position
=Clng(Split(Range,"-")(0))   
                Response.BinaryWrite(S.Read)   
              
End   if   
    
      
end   if   
    Response.Flush
    S.Close
    
Set S = Nothing        
    
end   function   
%
> 

 

 

这个方法在下载小文件(小于4MB)的时候是没有问题的,但是下载超过4MB的文件时就报500错误

经查询MSDN,原来microsoft早有解决方案

连接如下:

http://support.microsoft.com/kb/944886/zh-cn

 

原来Response BUFF有个大小限制,默认是4MB,我们只要将这个限制加大就行了

查询默认的大小的步骤如下:

1.单击 开始 ,单击 运行 ,键入 cmd ,然后单击 确定

2.键入下面的命令,然后按 Enter 键: cd / d %systemdrive%"inetpub"adminscripts

3.键入下面的命令,然后按 Enter 键: cscript.exe adsutil.vbs GET w3svc / aspbufferinglimit

 

那么我们将它这个限制改大的点,步骤如下:

1.单击 开始 ,单击 运行 ,键入 cmd ,然后单击 确定

2.键入下面的命令,然后按 Enter 键: cd / d %systemdrive%\inetpub\adminscripts

3.键入下面的命令,然后按 Enter 键: cscript.exe adsutil.vbs SET w3svc / aspbufferinglimit LimitSize

  LimitSize 表示缓冲的限制大小,以字节为单位)。 是例如数 67108864 还设置为 64 MB 的缓冲的限制大小。

 

注意:

        这个也不能改的太大,不然你的服务器是吃不消的,这是因为会占用你很大的内存

 

 

【上篇】
【下篇】

抱歉!评论已关闭.