Fahad Amin
Fahad Amin

Reputation: 93

ASP.Net FTP Upload Not Working

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.

Upvotes: 1

Views: 357

Answers (1)

Ondrej Svejdar
Ondrej Svejdar

Reputation: 22054

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
}

Upvotes: 1

Related Questions