Alternative to Response.TransmitFile for transferring files via HTTP
11,142
Solution 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();
}
Solution 2
- Could you redirect the user to the URL on the other server?
- 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.
Author by
mancmanomyst
Updated on July 11, 2022Comments
-
mancmanomyst over 1 year
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?
-
Adam Sills over 14 yearsAnd note the API for that is Reponse.BinaryWrite
-
mancmanomyst over 14 yearsI can't redirect because the user needs to be prompted to save the file in each instance - even if the download is an image that can normally be opened in a web browser. Could you give me an example of how to proxy the request? I'm not sure I understand what you mean! Thanks for your suggestions
-
Ashwani over 14 yearsIf the only requirement is that the user should be prompted to save the file, then just add a custom HTTP header to your folder through the IIS MMC and set the "Content-Disposition" to "attachment" like you're doing in C#. Then a plain redirect to the file would work.