Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am trying to browse file and upload to folder then download it
upload and download from the same button
C#
string fileSavePath = Server.MapPath("~/uploads/");
FileUpload1.PostedFile.SaveAs(fileSavePath + fileName);
File.Copy(fileSavePath + fileName, Server.MapPath("~/processed/") + fileName, true);
//Response section
Response.ContentType = "application/zip";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.TransmitFile(Server.MapPath("~/processed/" + fileName));
Response.End();
//
Label1.Text = "File successfully uploaded";

then I want to write File successfully uploaded in the label
label text never change to File successfully uploaded
when I comment Response section text change successfully
why when use Response never write on page ?
Thanks..
Posted
Updated 10-Nov-15 20:46pm
v2

It's not the Response.TransmitFile that causes this: it's the Response.End
This terminates the stream and sends it to the client, so anything you do after that is ignored.

In fact, MS don't recommend Response.End anyway: https://support.microsoft.com/en-us/kb/312629[^]
 
Share this answer
 
Comments
Heba Kamel 11-Nov-15 4:10am    
thanks,
but how by using Response.Redirect ("nextpage.aspx", false);
redirect on the same page, I don't have two pages
how it appear label text ?
Do I understand you well
OriginalGriff 11-Nov-15 4:33am    
Response.Redirect throws away the current page and start a new one - so nothing below that works either, and the page content you loaded is junked as well...

Just take it out, and leave it out!
Heba Kamel 11-Nov-15 4:43am    
thanks,
any way I did it using hyperlink to download the file and used javascript to hide and show this hyperlink
thanks
When you submit the form and instigate the request what your asp.net code is doing is sending a response. When you're doing a normal page, asp.net will generate html from your controls and write that response as text\html and that's the response, it appears in the browser. When you set your own content type as a zip file and use transmitfile then you're taking over from asp.net and sending your own custom response, so the browser is offered to download the file. A request can only have one response....you can't send a file *and* send html. If you want one request to have two responses (a download and html) then you'll need to employ some trickery.

A common technique is to send your page as normal with your text, and in that page put a hidden iframe with the src pointing to a page that does the transmitfile. That way they see the message, and then get the file download prompt.

XML
<iframe src="download.aspx?file=filenametodownload" width="1" height="1" style="display:none;"></iframe>


Your download.aspx will then read the filename and instigate the transmitfile.
 
Share this answer
 
Comments
Heba Kamel 11-Nov-15 4:42am    
Thanks,
I made it the same idea by using hyperlink to download file
Member 12133729 13-Nov-15 23:14pm    
Please show the code as I am facing the same problem and can't get the solution
:(
Heba Kamel 16-Nov-15 6:34am    
Hi,
That's all I did

string fileSavePath = Server.MapPath("~/uploads/");
FileUpload1.PostedFile.SaveAs(fileSavePath + fileName);
File.Copy(fileSavePath + fileName, Server.MapPath("~/processed/") + fileName, true);

HyperLink link = new HyperLink();
link.ID = "Link1";
link.Text = fileName;
link.NavigateUrl = "download.aspx?name=" + fileName;
Page.Controls.Add(link);
Page.Controls.Add(new LiteralControl("<br/>"));
Label1.Text = "File successfully uploaded";

and there is download.aspx page to let me download from link

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900