Response.WriteFile

2020-03-24 09:38发布

问题:

There is one URL with 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 textbox and presses the download button.In the click event the Response.WriteFile is called which sends the file to the client.

Now I want to create another website with a page, in which the user enters the filename and presses the download button to download that file.

Now I want to utilise the first URL for that. I dont want to use Response.Redirect, because by that way, the user will come to know that I am using mydownload.com.

How can I acheieve that.

One way is : When we download something from microsoft's website, a small popup window(with no close, maximise and minimise button) and then the save dialog box appears.

How to achieve this or another to achieve the same ?

回答1:

You could first download the file from the 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 don't want to save the file temporary to the disk you could use the DownloadData method and then stream the buffer into the 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();
}


标签: asp.net