I need download files from folder on the server.
I have tried this code in ASP Classic, but when the file is DWG the download not working.
I don't have error, this code downloading all files type is out for DWG files.
Can anybody help me?
Thanks in advance.
My code below.
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
You need to add the mime type for dwg files on the server.
Uncommon file extensions often dont have an associasion.
Press new and depending on extension type and in your case my guess after a short google search would be that .dwg should have application/acad as mime type.
Hope this helps.