1

I am trying to upload an image file via FTP in ASP.Net. The image file is uploaded to correct location but when I read or download it, it's corrupt. My code is given below

protected void FTPUpload()
{
    //FTP Server URL.
    string ftp = ConfigurationManager.AppSettings.Get("FTPServer");

    //FTP Folder name. 
    string ftpFolder = "images/logos/";

    //FTP Credentials
    string ftpUsername = ConfigurationManager.AppSettings.Get("FTPUsername");
    string ftpPassword = ConfigurationManager.AppSettings.Get("FTPPassword");

    byte[] fileBytes = null;

    //Read the FileName and convert it to Byte array.
    string fileName = Path.GetFileName(fuLogo.FileName);
    using (StreamReader fileStream = new StreamReader(fuLogo.PostedFile.InputStream))
    {
        fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd());
        fileStream.Close();
    }

    try
    {
        //Create FTP Request.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + fileName);
        request.Method = WebRequestMethods.Ftp.UploadFile;

        //Enter FTP Server credentials.
        request.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
        request.ContentLength = fileBytes.Length;
        request.UsePassive = true;
        request.UseBinary = true;
        request.ServicePoint.ConnectionLimit = fileBytes.Length;
        request.EnableSsl = false;

        using (var requestStream = request.GetRequestStream())
        {
            CopyStream(fuLogo.PostedFile.InputStream, requestStream);
        }

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        //lblMessage.Text += fileName + " uploaded.<br />";
        response.Close();
    }
    catch (WebException ex)
    {
        throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
    }
}

public static void CopyStream(Stream input, Stream output)
{
    byte[] buffer = new byte[1024000];
    int read;
    while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
    {
        output.Write(buffer, 0, read);
    }
}

Am I missing something? The file is uploaded perfectly but for some reason it gets corrupted.

1 Answer 1

1

This part smells:

using (StreamReader fileStream = new StreamReader(fuLogo.PostedFile.InputStream))
{
    fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd());
    fileStream.Close();
}

There should be no reason to first copy image to array (and use default text encoding/UTF8 decoding in the meantime) - just do stream-to-stream copy
(see How do I copy the contents of one stream to another?):

public static void CopyStream(Stream input, Stream output)
{
    byte[] buffer = new byte[32768];
    int read;
    while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
    {
        output.Write (buffer, 0, read);
    }
}

So the ftp upload should be just

using (var requestStream = request.GetRequestStream())
{
   CopyStream(fuLogo.PostedFile.InputStream, requestStream);
   // no need to requestStream.Close(); - using does that for you
}
Sign up to request clarification or add additional context in comments.

4 Comments

The problem is still there
@FahadAmin Show us your updated code, so that we can check that you have applied Ondrej's suggestion correctly.
Just edited the code in original post. CopyStream() method is exactly same as suggested by Ondrej
I'm sorry I didn't understand the solution completely first. Removing following block of code has resolved the issue using (StreamReader fileStream = new StreamReader(fuLogo.PostedFile.InputStream)) { fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd()); fileStream.Close(); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.