简体   繁体   中英

download file from url on other Server "Host Download" - asp.net mvc

For downloading files from my server I use this method in asp.net MVC:

string fileName = "SAMSUNG.zip";
string path = @"D:\Tutorial MVC5\ContosoUniversity\ContosoUniversity\dlfile\";
string fullPath = path + fileName;
Response.Clear();
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.ContentType = "application/x-zip-compressed";
Response.WriteFile(fullPath);
Response.End();

But, what about when I want to download files from another server, for example from a "host download". How can I do that? For example my download direct link is: http://dl.test.com/file.zip , now user clicks a link <a href="http://test.com/1">file.zip</a> is to download the file. Now I want to send file.zip to the user without a user knowing where my trusted link is and where my host download is. She or he just chooses the file to download.

Thanks for help!

are you familiar with how to use and or Set Request.Params your url originally should look like this if you want to check the QueryString

http://dl.test.com?file_name=SAMSUNG.zip

if(Request.Params["file_name"] == "SAMSUNG.zip"
{
    Uri uri = new Uri("http://dl.test.com/file.zip");
    using (var wc = new WebClient())
    using (var download = wc.OpenRead(uri))
    using (var respStream = Response.OutputStream)
    {
        download.CopyTo(respStream);
    }
}   

Hello i used the next Code in .net Core 5

[HttpGet("Descargar")]
public FileResult Descargar(string url,string nombre)
{
    Uri uri = new Uri(url);
    WebClient wc = new WebClient();
    Stream downloadStream = wc.OpenRead(uri);
    MemoryStream ms = new MemoryStream();
    downloadStream.CopyTo(ms);
        
    return File(ms.ToArray(), System.Net.Mime.MediaTypeNames.Application.Octet, nombre);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM