views:

175

answers:

1

Hi, I was wondering if anyone knows the answer to why my code casts an exeption when I do the fileStream.Read call, with the message "The stream is disposed"

I can list the files in the folder and using my browser to the fileUrl works fine. No proxies is involved. UnifiedFile is just an subclass of VirtualFile which comes from the CMS i work with. (it's an image that i'm trying to download)

FtpWebRequest fileRequest = (FtpWebRequest)FtpWebRequest.Create(fileUrl);
fileRequest.Method = WebRequestMethods.Ftp.DownloadFile;
fileRequest.Credentials = credentials;
fileRequest.EnableSsl = this.EnableSsl;
fileRequest.UseBinary = true;
fileRequest.UsePassive = true;
fileRequest.KeepAlive = true;
fileRequest.Proxy = null;

using (FtpWebResponse fileResponse = (FtpWebResponse)fileRequest.GetResponse())
{
    using (Stream fileStream = response.GetResponseStream())
    {
        UnifiedFile createdFile = PictureManager.Instance.CreateOriginalFile(name);
        using (Stream targetStream = createdFile.Open(FileMode.OpenOrCreate))
        {
             byte[] buffer = new byte[2048];

             // Read the file
             while (true)
             {
                int bytesRead = fileStream.Read(buffer, 0, buffer.Length);
                // Reached end of stream, close
                if (bytesRead == 0)
                  break;

                targetStream.Write(buffer, 0, bytesRead);
              }

         }
   }

}

Edit: Found the bug; the "GetResponseStream" is done to the previous request and therefor wrong request. doh

Thanks Guys for the "Granny help" ;-) (Swedish expression so don't know if it really applies in english)

+3  A: 

You should be checking the response StatusCode before continuing. I suspect that the request has failed so there is no response body.

AnthonyWJones
StatusCode was = "DataAlreadyOpen"
ullmark
And the exact exception message is: "Cannot access a disposed object. Object name: 'System.Net.Sockets.NetworkStream'"
ullmark