下载文件适用于Firefox,而不适用于其他浏览器

时间:2016-12-13 14:15:50

标签: asp.net vb.net download header response

在暂存我的下载功能工作正常,当我在生产中部署(使用IIS 7.5的nas服务器)时,相同的功能仅适用于Firefox。 随着IE,Edge和Chrome的下载开始,但是它不会到达终点,并且在一分钟之后会出现错误。 我知道网上有很多文档,但我只是尝试了很多没有结果的解决方案。请帮帮我。

@Page Language="VB" ContentType="text/html"

@Import Namespace="System.Data"

@Import Namespace="System.Data.SqlClient"

@Import Namespace="System.IO"


Private SUB SDataBind()

    'Path construction
        Dim VarFileName as string = "mytest.zip"

    'Path construction
        Dim VarPath as string = "/public/mydownload" & VarFileName

    'Absolute path
        Dim VarCompletPath as string = Server.MapPath(VarPath)
        VarCompletPath = replace(VarCompletPath,"/","\")

    'Filename construction
        Dim VarLast4Char as string = right(VarFileName,4)
        Dim VarNewFilename as string = "download" & VarLast4Char

    'Headers cleaner
        Response.Clear
        Response.ClearHeaders
        Response.ClearContent

    'Send the file to the browser
        Response.AddHeader("Content-Disposition", ("attachment;filename=" & VarNewFilename))
        Response.ContentType = ReturnFiletype(  right(VarFileName,4)  )
        Response.WriteFile( VarCompletPath )
        Response.Flush()
        Response.Close()
        Response.End

end sub


'----------------------------

'Get content file type
Private function ReturnFiletype(fileExtension as string) as string  

    Select Case fileExtension

        case ".zip"
            return "application/zip"
        case ".pdf"
            return "application/pdf"
        case else
            return "application/octet-stream"
        end select

end function

1 个答案:

答案 0 :(得分:0)

我解决了更经典的功能

'清理标题     Response.Clear     Response.ClearHeaders     Response.ClearContent

'将文件发送到浏览器     Response.AddHeader(" Content-Disposition",(" attachment; filename ="& VarFileName))     Response.WriteFile(使用Server.Mappath(VarPath))     到Response.End

我删除了这些行    Response.Flush()    Response.Close()