Force download file (ASP)
Any file on a web server can be downloaded to the client. The IUSR_MACHINENAME (or Everyone) account needs read access to that file for it be be downloaded. MDAC 2.5 or later is needed for this to work - http://www.microsoft.com/data/download_250rtm.htm
JScript:
// clear buffer Response.Buffer = true; Response.Clear(); // path to file var sFilePath = Server.MapPath("/path/to/file.pdf"); // attachment name (without extension), this will be what the name of the downloaded file will be var sAttachName = "attachmentname"; // remove spaces, else file may not download sAttachName = sAttachName.replace(/\s/g,""); // create filesystem object var oFSO = Server.CreateObject("Scripting.FileSystemObject"); // check if file exists if(!oFSO.FileExists(sFilePath)){ Response.Write('<strong>File does not exist. <a href="'+Request.ServerVariables("HTTP_REFERER")+'">Go back</s>.</strong>'); Response.End(); } // get file var oFile = oFSO.GetFile(sFilePath); // get file size var nFileSize = oFile.Size; // get extension var sExt = sFilePath.substring(sFilePath.lastIndexOf(".")); // download as attachment Response.AddHeader("Content-Disposition","attachment;filename="+sAttachName+sExt); // add content length header (for showing progress of download) Response.AddHeader("Content-Length", nFileSize); // set to always download by setting content type Response.ContentType = "application/octet-stream"; // create stream var oStream = Server.CreateObject("ADODB.Stream"); // open stream oStream.Open; // set type of stream to binary oStream.Type = 1; // load data from file into stream oStream.LoadFromFile(sFilePath); // send data to client Response.BinaryWrite(oStream.Read); // close stream and end output to client oStream.Close; Response.End();
VBScript:
Option Explicit ' declare variables Dim sFilePath, sAttachName, oFSO, oFile, nFileSize, sExt, oStream ' clear buffer Response.Buffer = True Response.Clear ' path to file sFilePath = Server.MapPath("/path/to/file.pdf") ' attachment name (without extension), this will be what the name of the downloaded file will be sAttachName = "attachmentname" ' remove spaces, else file may not download sAttachName = Replace(sAttachName," ","") ' create filesystem object Set oFSO = Server.CreateObject("Scripting.FileSystemObject") ' check if file exists If oFSO.FileExists(sFilePath) = False Then Response.Write("<strong>File does not exist. <a href="""&Request.ServerVariables("HTTP_REFERER")&""">Go back</s>.</strong>") Response.End End If ' get file Set oFile = oFSO.GetFile(sFilePath) ' get file size nFileSize = oFile.Size ' clear file system object Set oFile = Nothing Set oFSO = Nothing ' get extension sExt = Right(sFilePath, (Len(sFilePath) - InStrRev(sFilePath,".",-1,1) + 1) ) ' download as attachment Response.AddHeader "Content-Disposition","attachment;filename="&sAttachName&sExt ' add content length header (for showing progress of download) Response.AddHeader "Content-Length", nFileSize ' set to always download by setting content type Response.ContentType = "application/octet-stream" ' create stream Set oStream = Server.CreateObject("ADODB.Stream") ' open stream oStream.Open ' set type of stream to binary oStream.Type = 1 ' load data from file into stream oStream.LoadFromFile sFilePath ' send data to client Response.BinaryWrite oStream.Read ' close stream and end output to client oStream.Close Set oStream = Nothing Response.End
Comments