Alternative to Response.TransmitFile for transferring files via HTTP

元气小坏坏 提交于 2020-01-23 09:47:48

问题


I'm working on a ASP.NET website that allows users to download files.

Previously the files were stored on the same server as the website so we could do:

Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
Response.AddHeader("Content-Length", response.ContentLength.ToString());
Response.ContentType = "application/octet-stream";
Response.TransmitFile(path);
Response.End();

However, now some of the files are stored on a seperate server. I can verify that the files exist using

WebRequest request = WebRequest.Create(absolute-url);
WebResponse response = request.GetResponse();

But how can I facilitate the transfer as TransmitFile requires a virtual path not a url?

I need the users to be able to choose where to Save the file as with a normal web download

What's the best way to do this?


回答1:


If you can get the response stream via a web request you should be able to copy the stream to your output stream as per this snippet:

while ((read = stream.Read(buffer, offset, chunkSize)) > 0)    
{

    Response.OutputStream.Write(buffer, 0, read);
    Response.Flush();
}



回答2:


You can't use TransferFile for remote file. But you can use WriteFile for this.




回答3:


  1. Could you redirect the user to the URL on the other server?
  2. You could proxy the request to the other server. When you call "GetResponse", take the stream and write its contents out to your Response object.



回答4:


You could map the drives of the remote servers as shares and then use TransmitFile. If the servers don't have line of sight you could enable WebDAV on the remote server(s) and then map them to a physical path and use TransmitFile.



来源:https://stackoverflow.com/questions/1155183/alternative-to-response-transmitfile-for-transferring-files-via-http

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