简体   繁体   English

来自外部链接的ashx中的asp.net C#下载文件

[英]asp.net C# download file in ashx from external link

need some help in downloading file with ashx 在使用ashx下载文件时需要一些帮助

i'm trying to download large file (about 2-4GB) from external link (file not stored on webserver) 我正在尝试从外部链接下载大文件(大约2-4GB)(文件未存储在网络服务器上)

here is my code 这是我的代码

context.Response.Clear();
context.Response.ContentType = "video/mp4";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
context.Response.Write("http://otherserver/file.m4v");
context.Response.Flush();
context.Response.Close();

and downloaded file is 1kb what i;m doing wrong? 并且下载的文件为1kb,我做错了什么? and is other way to download file? 还有其他下载文件的方式吗? I'm trying to force browser to download file (and change filename) not to preview in brower 我试图强制浏览器下载文件(并更改文件名),以免在浏览器中预览

PS sory for my english ;) PS抱歉我的英语;)

This is an incorrect approach. 这是不正确的方法。 The file content will be: 文件内容为:

http://otherserver/file.m4v

Which you are setting here: 您在这里设置的:

context.Response.Write("http://otherserver/file.m4v");

What you need to use is the HttpWebRequest Class . 您需要使用HttpWebRequest类

All you're doing is sending the browser a file containing the text "http://otherserver/file.m4v" , with a header suggesting that the browser offers to download, rather than display, the file. 您要做的只是向浏览器发送一个包含文本"http://otherserver/file.m4v" ,并带有一个标题,表明浏览器提供下载而不是显示文件。

There's no magic going on in the browser that causes it to say "Oh, I should download whatever's at that URL" when it sees a file with a URL in it. 当浏览器看到其中包含URL的文件时,浏览器中没有任何魔术使它说“哦,我应该下载该URL上的内容”。

Moreover, having Googled around a bit and looked at several PHP discussions on this subject, I don't think there's a way to do what you want without literally streaming the file from the remote URL onto your server, and then sending it on from your server to the client. 此外,经过一段时间的Google搜索并查看了有关此主题的几个PHP 讨论 ,我认为没有一种方法可以做,而不必将文件从远程URL实际流传输到服务器上,然后再从您的服务器上发送出去。服务器到客户端。

You could try adding the header and then sending a redirect to the client, but I'd expect the client to discard the header when it makes the request to the remote URL - and so display the result in the browser. 您可以尝试添加标头,然后将重定向发送给客户端,但是我希望客户端在向远程URL发出请求时会丢弃标头-从而在浏览器中显示结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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