0

I've a site set up in IIS. It's allows users to download files from a remote cloud to their own local desktop. HOWEVER, the context seems to be mixed up, because when I access the website externally via the IP, and execute the download, it saves the file to the server hosting the site, and not locally. What's going on??

My relevant lines code:

using (var sw2 = new FileStream(filePath,FileMode.Create))
            {           
                    try
                    {
                        var request = new RestRequest("drives/{chunk}");

                        RestResponse resp2 = client.Execute(request);

                       sw2.Write(resp2.RawBytes, 0, resp2.RawBytes.Length);
                    }                    
                }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user756678
  • 13
  • 6

2 Answers2

1

Your code is writing a file to the local filesystem of the server. If you want to send the file to the client, you need to do something like

Response.BinaryWrite(resp2.RawBytes);

The Response object is what you use to send data back to the client who made the request to your page.

Jason
  • 86,222
  • 15
  • 131
  • 146
  • Response.BinaryWrite doesn't seem to work. The file gets created, but doesn't get any data written to it. Any suggestions? – user756678 May 22 '11 at 02:02
  • Are you sure that resp2 has the data you expect in it? – Jason May 22 '11 at 02:22
  • Actually, it creates the file - on the server still - but no data gets written to it. – user756678 May 22 '11 at 02:49
  • I'm positive, since it works perfectly when it saves to the server. – user756678 May 22 '11 at 03:00
  • How does Response.BinaryWrite know which file to write to? – user756678 May 22 '11 at 07:12
  • See http://stackoverflow.com/questions/848679/reading-a-binary-file-and-using-response-binarywrite for a more complete example of using BinaryWrite. – Jason May 22 '11 at 13:12
  • @Jason, I'm still missing something. How does the actual file get written to? 'using(WebResponse response3 = req.GetResponse()) {using (Stream responseStream = response3.GetResponseStream()) { using (MemoryStream memoryStream = new MemoryStream()) { int count = 0; do { count = responseStream.Read(buffer, 0, buffer.Length);' – user756678 May 23 '11 at 01:09
  • memoryStream.Write(buffer, 0, count);} while (count != 0); Response.Clear(); Response.Buffer = true;Response.AddHeader("content-disposition", String.Format("attachment;filename={0}", Path.GetFileName(filePath)));Response.ContentType = ("text/plain"); Response.BinaryWrite(memoryStream.ToArray()); offset += memoryStream.Length; bytesRemaining -= memoryStream.Length; } } } – user756678 May 23 '11 at 01:13
  • gosh I don't know how else to post a new chunk of code. Sorry! – user756678 May 23 '11 at 01:13
  • "How does the file get written to?" The file on the client? The page sends the response to the client's browser, and the browser is in charge of actually saving it. You should really try creating a simple web page that does nothing but send a file to the client. Once you get that working then you can add in the complexity of reading the file from another system before sending it. And you should edit your original question to add detail, not stuff code into the comments. – Jason May 23 '11 at 02:15
0

I imagine that code snippet you posted is running in some sort of code-behind somewhere. That is running on the server - it's not going to be running on the client. You will need to write those bytes in the Response object and specify what content-type, etc. and allow the user to Save the file himself.

Dismissile
  • 32,564
  • 38
  • 174
  • 263
  • I have the user type in the destination location, and use that as the filepath. Are you saying I need to use something other than the FileStream object? – user756678 May 20 '11 at 19:29
  • It doesn't matter what the user types in. That code is executing on the SERVER. – Dismissile May 20 '11 at 20:39