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

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.

Related

Use Response.Redirect to open pdf in browser

How to dynamically open file resource in browser using Response call in C# and ASP.NET?
I have a file server that stores documentation. I have another server that runs IIS and ASP.NET application. I need to open a pdf document stored on the file server in a browser window.
I supply folderPath string as "\\MyFileServer\documentFolder\" and filename string as "doc.pdf". I try to redirect to
Response.Redirect(folderPath + filename, true);
but redirection tries to append my provided location of the resource to current server path. I'm new to ASP.NET so please forgive me my ignorance.
Oh, BTW, the site is accessed from android tablets so I need explicitly redirect client browser to a file resource, otherwise android will start downloading the file instead of opening it.
In a response, write a Header:
Content-Disposition: inline; doc.pdf
See more here
Have a look at Response.AddHeader
You can do something like
Response.AddHeader("content-disposition", "inline; filename=" + filepath + ".pdf");

cannot redirect after downloading a file

I have to redirect to the same page after a file download !
when i say response.redirect .. it says Cannot redirect after HTTP headers have been sent
can some one help me with this?
i am zipping a file using ionic.zip and downloading it.
PS : Let me know if i have to make my question more clear i can explain :(
Response.Clear();
Response.BufferOutput = false;
string filename = "results" + ".zip";
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "filename=" + filename);
using (ZipFile zip = new ZipFile())
{
zip.AddDirectory(pathhdf.Value);
zip.Save(Response.OutputStream);
}
Response.Close();
Response.Redirect("Default.aspx"); /// here come my error
Thanks in advance!
You can't send a redirect header, or any other HTTP headers, after you send HTTP content. In this case your HTTP content is your .zip file, so you can't send a redirect header after you write the .zip file to the output stream.
You cannot redirect from the server because you don't know how long it will take for the client to download the file. You could use javascript interval to poll for the presence of a cookie that the server could emit. Here's a nice article explaining this. Once the cookie is detected the client knows that download has finished and you could redirect using for example window.location.href. And here's a similar post.
You can't make a redirect after sending a file. There can only be one response for one request, and the redirect is a response in itself.
If you want to make both the download and the redirect, you have to send two requests from the client. Start the download, then use setTimeout to make the redirect to Default.aspx after a delay long enough to know that the donload has started. The server will only reply to one request at a time, so the Default.aspx page will load after the download is complete.

Downloading files in an Android 3.0 browser from my ASP.NET site

I'm having an issue downloading a PDF file from my custom asp.net site in the out of the box browser on an Android tablet (a Motorola Xoom).
I'm using Response.OutputStream.Write to send the file because it's having to read the file from a UNC path. This method works on IE, Firefox, Safari, and on iPad, but doesn't work on the browser on the Xoom. However, when I downloaded Firefox on the Xoom I was able to download the file just fine.
I've found a couple of spots suggesting I use the following headers:
Content-Type: application/octet-stream
Content-Disposition: attachment; filename="MyFileName.PDF"
I've tried this with no luck. I also tried using Content-Types of application/pdf and application/force-download and every combination of filename capitalized, extension capitalized, extension lowercase, double quotes, single quotes, no quotes, etc... for Content-Disposition and have yet to find anything that works.
I have also found that if I execute the code to download the file on Page_Load it is able to download, but if I do a postback and then execute the code (clicking on a link button to download the file) it doesn't work
Has anyone been able to download a file using custom C# code on the OOTB Android 3.0 browser?
This is old but in case anyone comes across this issue:
Check unknown sources under settings --> applications.
Make sure you have PDF defined in MIME Types (application/pdf) on the IIS server.
Also if you are using a file handler you will need to set the header info.
This code block is an example of what would be used in a file handler:
string dirpath = System.Configuration.ConfigurationManager.AppSettings["FilePath"];
string file = System.Configuration.ConfigurationManager.AppSettings["FileName"];
System.IO.FileInfo fi = new System.IO.FileInfo(dirpath + file);
string contentlen = fi.Length.ToString();
context.Response.Buffer = true;
context.Response.Clear();
context.Response.AddHeader("content-disposition", "attachment; filename=" + file);
context.Response.AddHeader("content-length", contentlen);
context.Response.ContentType = "application/pdf";
try
{
context.Response.WriteFile(dirpath + file);
}
catch (Exception)
{
context.Response.Redirect("Error.aspx");
}
I have no idea as to why this is happening, but if you can download a file inside the Page_Load block, the a workaround would be linking to a dedicated page for downloading where you call the download function inside the Page_Load block.
To make this more compliant with the rest of your website (if needed) you might pass different parameters to the page for downloading different files, or redirect to this page only if you detect the site is running on the android browser.
I will add code examples shortly.

How to force file download on Telerik RadEditor?

I am using Document Manager in Telerik RadEditor.
Once I upload .txt file and click on that link, it opens on the browser instead of downloading it. How to force download that file without going in to .htaccess or other server changes?
Short answer is: using RadEditor alone you can't.
In order to make a browser-viewable file type be served as a download you must send it to the client's browser with a 'Content-Disposition' type of 'attachment'. Doing so is fairly simple, however it requires server-side code that would be outside of the scope of RadEditor.
var bytes = System.IO.File.ReadAllBytes(Server.MapPath("~/path/to/file.txt"));
Response.AddHeader("Content-Type", "text/plain");
Response.AddHeader("Content-Displosition", "attachment;filename=file.txt;size=" + bytes.Length);
Response.Flush();
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
Unless you want to write a specific handler for serving the file in question, your only option is to instruct users that they must 'Right-Click > Save Link As...' on your text file link.

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

Categories