Download office document without the web server trying to render it - c#

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()

Related

Accessing downloaded file instead of page HTML

I have some code that connects to an HTTP API, and is supposed to get an XML response. When the API link is placed in a browser, the browser downloads the XML as a file. However, when the code connects to the same API, HTML is returned. I've told the API owner but they don't think anything is wrong. Is there a way to capture the downloaded file instead of the HTML?
I've tried setting the headers to make my code look like a browser. Also tried using WebRequest instead of WebClient. But nothing works.
Here is the code, the URL works in the browser (file downloaded) but doesn't work for WebClient:
WebClient webClient = new WebClient();
string result = webClient.DownloadString(url);
The code should somehow get the XML file instead of the page HTML (actually the HTML doesn't appear in the browser, only the file).
The uri that you access may be a HTML page that have its own mechanism (like generating the actual download address which may be dynamically generated by the server and redirecting to it, in order to prevent external linking access) to access the real file.
It is supposed to use a browser background browser core like CefSharp to run the HTML and its javascript to let it navigate, and probably you may want to hook the download event to handle the downloading.
I think you need to add accept header to the WebClient object.
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.Accept] = "application/xml;q=1";
string result = webClient.DownloadString(url);
}
Thank you all for your input. In the end, it was caused by our vendor switching to TLS 1.2. I just had to force my code to use 1.2 and then it worked.

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>

asp.net C# download file in ashx from external link

need some help in downloading file with ashx
i'm trying to download large file (about 2-4GB) from external link (file not stored on webserver)
here is my code
context.Response.Clear();
context.Response.ContentType = "video/mp4";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
context.Response.Write("http://otherserver/file.m4v");
context.Response.Flush();
context.Response.Close();
and downloaded file is 1kb
what i;m doing wrong?
and is other way to download file?
I'm trying to force browser to download file (and change filename) not to preview in brower
P.S sory for my english ;)
This is an incorrect approach. The file content will be:
http://otherserver/file.m4v
Which you are setting here:
context.Response.Write("http://otherserver/file.m4v");
What you need to use is the HttpWebRequest Class.
All you're doing is sending the browser a file containing the text "http://otherserver/file.m4v", with a header suggesting that the browser offers to download, rather than display, the file.
There's no magic going on in the browser that causes it to say "Oh, I should download whatever's at that URL" when it sees a file with a URL in it.
Moreover, having Googled around a bit and looked at several PHP discussions on this subject, I don't think there's a way to do what you want without literally streaming the file from the remote URL onto your server, and then sending it on from your server to the client.
You could try adding the header and then sending a redirect to the client, but I'd expect the client to discard the header when it makes the request to the remote URL - and so display the result in the browser.

C# file download using WebBrowser

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.

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.

Categories