Response.WriteFile

There is one URL with a specific syntax to download a file.
http://www.hddownloader.com/?url=http://www.youtube.com/watch?v=N-HbxNtY1g8&feature=featured&dldtype=128

The user enters the file name in the text box and clicks the download button. In the click event, Response.WriteFile is called, which sends the file to the client.

Now I want to create another site with a page in which the user enters a file name and presses the download button to download this file.

Now I want to use the first URL for this. I do not want to use Response.Redirect because this way the user will know that I am using mydownload.com.

How can I accept this?

One way: when we download something from the Microsoft website, a small pop-up window (without the close, maximize and minimize button), and then the save dialog box appears.

How to achieve one or the other to achieve the same?

+4
source share
1 answer

You can first download the file from a remote location on your server using WebClient.DownloadFile :

using (var client = new WebClient()) { client.DownloadFile("http://remotedomain.com/somefile.pdf", "somefile.pdf"); Response.WriteFile("somefile.pdf"); } 

or if you do not want to save the temporary file to disk, you can use the DownloadData method and then stream the buffer in response.


UPDATE:

Example with the second method:

 using (var client = new WebClient()) { var buffer = client.DownloadData("http://remotedomain.com/somefile.pdf"); Response.ContentType = "application/pdf"; Response.AppendHeader("Content-Disposition", "attachment;filename=somefile.pdf"); Response.Clear(); Response.OutputStream.Write(buffer, 0, buffer.Length); Response.Flush(); } 
+5
source

Source: https://habr.com/ru/post/1336579/


All Articles