ullmark
ullmark

Reputation: 2479

FtpWebRequest says Stream is Disposed

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)

Upvotes: 4

Views: 2419

Answers (1)

AnthonyWJones
AnthonyWJones

Reputation: 189437

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

Upvotes: 3

Related Questions