cannot redirect after downloading a file - c#

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.

Related

How to redirect to another page after csv file download?

I have download button which after download i want to redirect to another page.
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=downloadfile.pdf");
Response.TransmitFile(Server.MapPath("downloadfile.pdf"));
Response.AddHeader("Refresh", "3;URL=redirectpage");
This is the code for downloading the file, right now after downloading the file is not redirecting to the page i want to redirect.
Depending on how the request for the file is made, you may want to consider using some client-side code to perform the redirection. A small example of a file download requested from a hyperlink can can be found here:
https://social.msdn.microsoft.com/Forums/sharepoint/en-US/58ff5ed5-c3af-40f9-b136-b0415a0c767c/redirect-after-file-download?forum=sharepointdevelopmentprevious
This example redirects after starting the file download.
You could also redirect then initiate the download as show here (context is with PHP but solution is HTML-based and JS-based)
PHP generate file for download then redirect

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");

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.

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