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

徘徊边缘 提交于 2019-12-12 06:46:26

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/33835563/download-file-from-url-on-other-server-host-download-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!