Force download rather than view

T

tma

How can I force a file to be downloaded to the client when the click a link
button, rather than show the file in the browser window (or another window).
I want the user to be prompted to open or save the file associated with the
link, not have it open automatically in their browser.


Thanks.
 
G

George Durzi

Search for Response.AddHeader "content-disposition" on MSDN
I was just doing something like this
 
J

Joerg Jooss

Chandra said:
Even adding a header like no-cache will do.

Why should that work? Most web applications will deliver non-cacheable
content -- if a click on any old hyperlink would force a download, the web
app would hardly be usable...
 
G

George Durzi

How would you do the opposite? If I want to display a .txt file in the
browser ...

Doing a

Response.ClearContent()

Response.ClearHeaders()

Response.ContentType = "text/plain"

Response.WriteFile(sExportPath)

Response.Flush()

Response.Close()



still prompts you to download the .aspx file, even if sExportPath is
blahblah\something.text
 
J

Joerg Jooss

George said:
How would you do the opposite? If I want to display a .txt file in the
browser ...

Doing a

Response.ClearContent()

Response.ClearHeaders()

Response.ContentType = "text/plain"

Response.WriteFile(sExportPath)

Response.Flush()

Response.Close()



still prompts you to download the .aspx file, even if sExportPath is
blahblah\something.text

That's completely up to the client.
 
G

Guest

You can encourage the UA to display the file with something like
Response.Headers.Add("Content-disposition","inline; filename=whatever.txt")
(Untested, so I might have the formatting on the parameters off a tad.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top