Download a file instead opening in browser
-----------------------------------
Why we need the functionality It is common for most of internet users when we click on any link on web page the content (e.g. image file , .doc , .ppt file etc.) open in same window if browser understand the format . User prompt for open/save dailog box for other document like .zip file.
Consider a page containing list of documents with hyperlink on document name for download , suppose user click on .doc file file will open in same windows and vanishes orignal list page .
If you want to always prompt for open/save file use following code for hyperlinks , ASP:Hyperlink
-----------------------------------
Save following code in a file with extention .asp
Save following code in a file with .asp extention. <% Response.Buffer=true fpath = request("fpath") fname = request("fname") if fname<>"" then Set objFSO = Server.CreateObject("Scripting.FileSystemObject") filename=fpath & "\" & fname if(objFSO.FileExists(filename))=true then Dim tfm_downloadStream Set tfm_downloadStream = Server.CreateObject("ADODB.Stream") tfm_downloadStream.Type = 1 tfm_downloadStream.Open tfm_downloadStream.LoadFromFile (fpath + "/" + fname) If Err.number = 0 Then Response.Clear Response.ContentType = "application/octet-stream" Response.AddHeader "Content-Disposition", "attachment; filename=" & fname Response.AddHeader "Content-Transfer-Encoding","binary" Response.BinaryWrite tfm_downloadStream.Read tfm_downloadStream.Close Set tfm_downloadStream = Nothing Response.End() Else tfm_downloadStream.Close Set tfm_downloadStream = Nothing Response.Write ("<HTML><HEAD><SCRIPT> ") Response.Write ("alert('Unexpected Error');") Response.Write ("javascript:window.history.back();") Response.Write ("</SCRIPT></HEAD></HTML>") Response.End End If else Response.Write ("<HTML><HEAD><SCRIPT> ") Response.Write ("alert('File Not Found');") Response.Write ("javascript:window.history.back();") Response.Write ("</SCRIPT></HEAD></HTML>") Response.End end if else Response.Write ("<HTML><HEAD><SCRIPT> ") Response.Write ("alert('File Not Found');") Response.Write ("javascript:window.history.back();") Response.Write ("</SCRIPT></HEAD></HTML>") Response.End end if %>
Now , create a hyperlink (say hyLnk) in calling .aspx page . Set navigate url of hylnk in Page_Load event
if ( !IsPostBack ) { string fname = "ImportSampleFile.xls"; string fpath= Server.MapPath ( ".\\" ); hyLnk.NavigateUrl = "ActionDownLoad.asp?fpath=" + Server.HtmlEncode(fpath) + "&fname=" + Server.HtmlEncode(fname) ; }
Here two parameters are passed in querystring :
1. fname : Name of file with extention. 2. fpath : Actual physical path of directory containing the file to be download.
-----------------------------------
That's it , enjoy code . ' Suggestions / comments are always welcome ' at 'akshaydubey@gmail.com'
|
| Author: Jack Haskell 03 Jan 2005 | Member Level: Bronze Points : 0 |
I thought this was supposed to be an ASP.NET article. Why use ASP code? I would like to see the same using .Net. Otherwise, a good example.
|