Force download rather than view

  • Thread starter Thread starter tma
  • Start date Start date
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.
 
Search for Response.AddHeader "content-disposition" on MSDN
I was just doing something like this
 
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...
 
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
 
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.
 
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

Back
Top