views:

2579

answers:

4

Hi Guys,

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  A: 
  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.
David
And note the API for that is Reponse.BinaryWrite
Adam Sills
I 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
mancmanomyst
If 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.
David
A: 

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.

Nissan Fan
A: 

I am using the following code in C#(ASP.NET) to download a word file: Response.ClearContent(); Response.AddHeader("content-disposition", "attachment;filename=" + file.Name); Response.ContentType = "application/vnd.word"; Response.Flush(); Response.TransmitFile(file.FullName); Response.End();

And I am getting a JScript Exception as Page Request Manager Parser Error Exception...it says that the error occurs when the request is modified by Response.Write(), response filters, HttpModules, or server trace is enabled. Details: Error parsing near 'PK...'

Can anyone give me a solution to it and explain me what exactly is tis error

A: 

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

Сварга