在 asp 经典上下载 DWG 文件

Download DWG files on asp classic

我需要从服务器上的文件夹下载文件。

我已经在 ASP Classic 中尝试过此代码,但是当文件为 DWG 时,无法下载。

我没有错误,此下载所有文件类型的代码不适用于 DWG 文件。

有人能帮帮我吗?

提前致谢。

下面是我的代码。

getfile.asp

<%
Function BaseName(byVal Path)
Dim Pos
    Path = Replace(Path,"/","\")
    Pos  = InStrRev(Path,"\")
    If Pos>0 then 
        BaseName = Mid(Path,Pos+1)
    Else
        BaseName = Path
    End if
end function

Function ReadFile(FileName)
Dim Stream , Path
    On error resume next
        Path = Server.MapPath(FileName)
        If Err.Number<>0 then Path = FileName
    On error goto 0
    Set Stream = Server.CreateObject("ADODB.Stream")
    Stream.Type=1
    Stream.Open()
    Stream.LoadFromFile Path
    Stream.Position=0
    ReadFile = Stream.Read()
    Stream.Close()
    Set Stream = Nothing
End Function

' Timeout
Server.ScriptTimeout=6000
if Len(Trim(request.querystring("file"))) > 0 then
    file = server.mappath(request.querystring("file")) 
else
    Response.Write("file not found")
    Response.end
end if
response.ContentType="application/octet-stream"
response.AddHeader "Content-Disposition", "attachment; filename=" & BaseName(file)
Response.BinaryWrite ReadFile(File)
Response.End
%>

default.asp

    <script language="JavaScript" type="text/javascript">
    function doDownload(file1, file2, frmName)
    {
      var ifrmObj = document.getElementById((frmName && frmName.length > 0) ? frmName : "dwnFrm1");
      ifrmObj.src = "";
      ifrmObj.src = "getFile.asp?file=" + file1;

      if (!file2 || file2.length <= 0) return;

      // Timeout
      window.setTimeout("doDownload('" + file2 + "', '', 'dwnFrm2');", 3000);
    }
    </script>

    <body bgcolor="#EAEFFF">

    <iframe id="dwnFrm1" style="display: none;"></iframe>
    <iframe id="dwnFrm2" style="display: none;"></iframe>

    extDWG = right(directoryfile.Name, 3)
    if extDWG = "dwg" then           
       response.write ("<a href=""javascript:void(0);"" onclick=""doDownload('/MyFolder/" & Server.HTMLencode(folder) & "/"& Server.HTMLencode(directoryfile.Name) &"', '/MyFolder/" & Server.HTMLencode(folder) & "/X-cart.dwg');"">")    
    else    
       response.write ("<a href=""/MyFolder/" & folder & "/"  & directoryfile.Name &""">")    
    end if

您需要为服务器上的 dwg 文件添加 mime 类型。

不常见的文件扩展名通常没有关联。

  • 在较旧的 IIS(版本 6/7)中,您右键单击 IIS 管理器中的服务器并选择属性,然后单击 MIME 类型按钮。此处需要为 .dwg 文件添加一条正确 mime 类型的记录。
  • 在 IIS 8 中,您单击 IIS 管理器中的网站名称,然后 select MIME 类型。然后以同样的方式添加,如下解释:

按 new 并根据扩展类型,在你的情况下,经过短暂的 google 搜索,我的猜测是 .dwg 应该 application/acad 作为 mime 类型。

希望对您有所帮助。