Using Server.TransferRequest to force download of file to browser - asp.net

I intend to use ASP.NET Server.TransferRequest to send a file to the browser without the browser knowing the actual path of the file being downloaded. For example, the address download.aspx?id=123 should result in the browsers Save dialog popping up.
When I use Server.TransferRequest to "redirect" to, say, a PDF file, the contents of the PDF file is sent to the browser; however, the content is shown in the browser window as - not surprisingly - unintelligible text.
I think there should be some headers attached to the response to force the browser to download the file, but "Content-disposition" header doesn't do the trick.
Any ideas on how to do this?

Related

Prevent download of MP3 files in wordpress

I know many people asks the opposite of what I am asking. I am trying to make browsers play the file instead of downloading when opening the mp3 files´ link. I do not care if the users download the file after that, but I need the browsers to play the file at first, instead of automatically downloading. It happens in computers and smarthphones.
, b
I don´t know if it is something I have to change in .htaccess file or somewhere else. The link to the site is this: https://cefadchurch.com/sermones/predicas-dominicales/
Try the download button. If you see, it directly sends you to the file´s link, but it starts downloading instead playing. I know a "Download" button function is obviously to make the browser download the file, but that will not be the case for me, because however, if the button sends me to the file´s link, I rather the browser play instead download, and the company has asked me for that function.
In your response headers, you're actively forcing the download in two ways:
Content-Disposition: attachment
Content-Type: application/octet-stream
Firstly, the Content-Disposition header value of attachment forces a download to occur. You must get rid of it if you want to not have a forced download.
Next, the content type of application/octet-stream is the generic binary type, and thus the browser doesn't know how to render it so just downloads it as a file to let the system handle it. If you're sending MP3s, use audio/mpeg for the Content-Type response header.
Finally, not all browsers can/will play media in a tab when you link to the media directly. If you want the file to play in-browser, you need to embed it into a web page with the <audio> tag.

jpg file rendering problem in IE?

we wrote CMS apps with asp.net. the user can attach file documents, pdfs to their forms and send to each other. the user can easily download the pdf and other documents but when they want to download jpg file their IE render the jpg file as a binary and show the binaries of jpg file? the IE didn't show the save as dialogue such as other files!
where is problem? and how can I handle this in asp.net? Is it relate to HTTPS? because the user used the https to connect to this cms.
As I understand it, users need to download the files and they are able to do so for some files but not for JPEG? And it shows as binary, what do you mean, it shows the byte codes?
Keep in mind each request has a content type header in the response which will identify to the browser what kind of content it is. This is set a known MIME type such as image/jpeg, ...
Content-Type: text/html; charset=utf-8
Browser loads each resource based on the content type. Having said that, it is up to the browser to load the resource the way it wants. I can personalise and change my browser setting to some point so you have to look at the user's browser if you cannot reproduce it on each machine.
In any case, you need to see the content-type header in the response for the resource (using Fiddler as someone else also suggested) and make sure it is set to a correct value. If it is set to image/jpeg, it will render it as image but if it is set to application/octet-stream it will download it. My hunch is it is set to something else (such as text/html or text/plain) and that is why browser tries to show it as a text.

Forcing the browser to pop a save as dialog box from a link pointing to remote url

I am building a web app that lets the user directly download files on a cdn by clicking a link. The link should point to the cdn url directly in order to minimize the load on our servers.
We would like the to have the browser pop up the save as dialog box when the user clicks the link to download the file and not have the browser display the content of the file at all. So the page should not reload. However, we don't have access to setting the HTTP headers sent back from cdn. Is it possible to still pop up the save as dialog box for download using client-side code?
Is it possible to still pop up the save as dialog box for download using client-side code?
No. Unless the file type is something the browser does not understand (or the HTTP header Content-Disposition is "attachment"), the "Save As" dialog will not appear.
This behavior cannot be changed by JavaScript.
The behavior is controlled by the Content-Disposition header, unless the browser simply doesn't understand how to display content of the type returned. Without the ability to change the Content-Disposition header to attachment, you can't force the browser to download the file instead of render it. This must be done server-side.

IE7/8 ignoring file download request in popup

I'm using Silverlight and I need to allow the user to save some dynamically genereated files.
For PDF files I created an http handler and it works just fine when I open it in a popup window.
For Excel files I tried every combination of Content-type and Content-disposition but IE8 refuses to open the file. With Fiddler I can see the get and there's a very short display of an IE window but it closes straight away.
I can't see any error message anywhere and I can't find any other description of the issue. IE7 exhibits the same behaviour.
I tried Content-type = application/vnd.ms-excel, application/unknown, application/octet-stream
and for for Content-disposition I tried inline and attachment.
PS: I can't use the SL built-in save dialog because it requires the context to be within a user action and I generate the file asychronously on the server.
Sounds like automatic prompting for downloads is disabled. See this description on how to enable automatic prompting. Enabling automatic prompting worked for me (situation: silverlight app uses Window.Navigate to open a popup to a generic handler that generates an excel file, with content-disposition: attachment, which worked fine in FF, but not in IE8).
As this post is already pretty old, I'm curious if you found a better way to solve this!
You should be using Content-Disposition: attachment
Have you tried on another client? My guess is maybe that Office is trying to get kicked off and is failing.
Alternatively, it's possibly related to http://blogs.msdn.com/ieinternals/archive/2009/10/02/Internet-Explorer-cannot-download-over-HTTPS-when-no-cache.aspx

Content-Disposition:What are the differences between "inline" and "attachment"?

What are the differences between
Response.AddHeader("Content-Disposition", "attachment;filename=somefile.ext")
and
Response.AddHeader("Content-Disposition", "inline;filename=somefile.ext")
I don't know the differences , because when I use one or another I always get a window prompt asking me to download the file for both of them. I read the specs, but it is clueless.
Because when I use one or another I
get a window prompt asking me to
download the file for both of them.
This behavior depends on the browser and the file you are trying to serve. With inline, the browser will try to open the file within the browser.
For example, if you have a PDF file and Firefox/Adobe Reader, an inline disposition will open the PDF within Firefox, whereas attachment will force it to download.
If you're serving a .ZIP file, browsers won't be able to display it inline, so for inline and attachment dispositions, the file will be downloaded.
If it is inline, the browser should attempt to render it within the browser window. If it cannot, it will resort to an external program, prompting the user.
With attachment, it will immediately go to the user, and not try to load it in the browser, whether it can or not.
It might also be worth mentioning that inline will try to open Office Documents (xls, doc etc) directly from the server, which might lead to a User Credentials Prompt.
see this link:
http://forums.asp.net/t/1885657.aspx/1?Access+the+SSRS+Report+in+excel+format+on+server
somebody tried to deliver an Excel Report from SSRS via ASP.Net -> the user always got prompted to enter the credentials. After clicking cancel on the prompt it would be opened anyway...
If the Content Disposition is marked as Attachment it will automatically be saved to the temp folder after clicking open and then opened in Excel from the local copy.

Resources