C# file download using WebBrowser - c#

Problem and solution is available at http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/d338a2c8-96df-4cb0-b8be-c5fbdd7c9202/?prof=required
This work perfectly if there is direct URL including downloading file-name.
But sometime some URL generate file dynamically. So URL don't have file name but after requesting that URL some website create file dynamically and then open/save dialog comes.
for example some link generate pdf file on the fly.
How to handle such type of URL?

If you have control over the server code, then you could ensure that it adds the Content-Disposition header to the HTTP response, such as the following:
Content-Disposition: attachment; filename=<put your filename here>
You could then look for that header on the client in order to decide to download the file in the background.
Even if you don't have control over the server code, check if the HTTP response already contains this header. I believe that this is a standard header that most browsers look for in order to know how to download a file.
This page indicates some guidelines for using the Content-Disposition header, including the following items:
The filename should be in US-ASCII
charset.
The filename should not have
any directory path information
specified.
The filename should not be
enclosed in double quotes even though
most browsers will support it.
Content-Type header should be before
Content-Disposition.
Content-Type
header should refer to an unknown
MIME type (at least until the older
browsers go away).
Scott Hanselman also has a small article on using this header.

Related

Is there a way to force an attachment to be either viewed in the browser, or downloaded?

Within our cross browser web app, the client can upload different file types (.pdf, .eml, .jpg, etc.).
The client has requested a standardized way of viewing these attachments, either by opening it in the browser, or downloading the attachment.
I have looked at the content-disposition header to force the download of the document, but will this work across all browsers? What about default browser applications, won't they try and open the file directly in the browser?
According to https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#Browser_compatibility content-disposition response header is compatible with all browsers. So, if you need to force a download use Content-Disposition: attachment (and the file will always be downloaded). It is recommended to specify a filename also, and probably a content type:
Content-Disposition: attachment; filename="filename.jpg"
Content-Type: image/jpg
If you skip the header, or just use content-disposition:inline then the browser will try to open the file inline.
For these cases, I use the download attribute on the tag.
<a href='./download-file.pdf' download='Name of File for user'>Click me to download</a>

What content-type do I use for http response headers that contain a file attachment of unknown file type?

Right now I am not specifying it and the system is defaulting to text/html which is causing some not so good results like downloading a movie on my iPhone is giving me a massive block of text so large that it crashes the browser for example. What content-type should I use? It could literally be any file type as the attachment. Content-disposition is attachment.
Use application/octet-stream.

How to identify whether the client machine supports PDF File format

Hii,
My requirment is to show a dynamically created pdf file directly to my web page. It works fine for the system which is having pdf reader software. But for the system which does not have the pdf software it is showing error like below
The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.
An invalid character was found in text content. Error processing resource 'http://localhost:4252/OmanePost/Customer/EBox/PD...
I need to handle this situation bit differently.i.e In this situation the file should be save to the physical location of the system for that i need to identify whether the client machine has pdf software or not then i can manage properly
I m using ASP.NET 2.0 version
It looks to me that you are serving your PDF with an XML mime/content-type. Make sure you set your content-type to application/pdf and you'll probably get a more suitable browser response.
In this case the browser should ask the user to open the file in an external application.
Please verify that you are sending the correct Content-Type: application/pdf header. Certain versions of Microsoft's browser ignore the content-type header, so you need to specify a filename ending in .pdf in the content disposition header: Content-Disposition: inline; filename=filename.pdf;
Note: I have not verified that it works with "inline" instead of "attachment", but I think it is worth a try.
My requirment is to show a dynamically created pdf file directly to my web page.
Try online ZohoViewer that takes a PDF file link and displays in the browser without requiring PDF reader on the client machine. As such there's no way to check if the client machine has a pdf reader or not.
You can not identify that client system has pdf software using javascript, asp.net, c#.
If the PDF reader software is not there and the PDF is a valid PDF then it should not throw exception. Instead it asks for a software in client machine which can read the file.

Download office document without the web server trying to render it

I'm trying to download an InfoPath template that's hosted on SharePoint. If I hit the url in internet explorer it asks me where to save it and I get the correct file on my disk. If I try to do this programmatically with WebClient or HttpWebRequest then I get HTML back instead.
How can I make my request so that the web server returns the actual xsn file and doesn't try to render it in html. If internet explorer can do this then it's logical to think that I can too.
I've tried setting the Accept property of the request to application/x-microsoft-InfoPathFormTemplate but that hasn't helped. It was a shot in the dark.
I'd suggest using Fiddler or WireShark, to see exactly how IE is sending the request, then duplicating that.
Have you tried spoofing Internet Explorer's User-Agent?
There is a HTTP response header that makes a HTTP user agent download a file instead of trying to display it:
Content-Disposition: attachment; filename=paper.doc
I understand that you may not have access to the server, but this is one straight-forward way to do this if you can access the server scripts.
See the HTTP/1.1 specification and/or say, Google, for more details on the header.
This is vb.net, but you should get the point. I've done this with an .aspx page that you pass the filename into, then return the content type of the file and add a header to make it an attachment, which prompts the browser to treat it as such.
Response.AddHeader("Content-Disposition", "attachment;filename=filename.xsn")
Response.ContentType = "application/x-microsoft-InfoPathFormTemplate"
Response.WriteFile(FilePath) ''//Where FilePath is... the path to your file ;)
Response.Flush()
Response.End()

Get redirected url from code

I'm using an API which, given a url, redirects to a file on the server. The file names have "_s,_m and _l" appended to the end (small, medium, large). However, since the url's querystring is parsed dynamically, I don't retrieve the actual file name. The image displays correctly, but is it possible to retrieve the filename of the image file from the code? (i.e. where the url has redirected to)?
e.g. http://api.somesite.com/getimage?small (this is what I enter)
"http://somesite.com/images/userimage_s.png" (this is where it redirects to. I would like to get this address from code)
Thanks for any advice
Sounds to me like you are trying to access some images you shouldn't access programmatically ;-)
You could access the given URL with an HTTP client (opening the stream with a stream reader might already suffice) and watch out for a Location header, which will most likely contain the URL you are searching for.

Categories