C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

Download a file instead opening in browser


Posted Date: 09 Dec 2004    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: Akshay DubeyMember Level: Bronze    
Rating: 1 out of 5Points: 7



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'



Responses

Author: Jack Haskell    03 Jan 2005Member 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.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Hide and show Data using Repeater control
Previous Resource: Runtime callable Wrapper
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use