mancmanomyst
mancmanomyst

Reputation: 2118

Alternative to Response.TransmitFile for transferring files via HTTP

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?

Upvotes: 3

Views: 11702

Answers (4)

TrystanC
TrystanC

Reputation: 441

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

Upvotes: 5

Сварга
Сварга

Reputation:

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

Upvotes: 2

Keith Adler
Keith Adler

Reputation: 21178

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.

Upvotes: 0

David
David

Reputation: 34573

  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.

Upvotes: 1

Related Questions