How can I upload an image using FtpWebRequest?
Asked Answered
I

2

5

I have been working at building a feature for a client that will enable them to upload images from www.site1.com, storing the image URL to the database and storing the images to a file on www.site2.com/images. I have managed to upload a file to the target location, but when I try to open and view the image, it is said to contain errors. I've never really had to work with images, so I'm at a loss.

This is the method used to upload the files:

public static void UpLoadImage(Stream image, string target)
    {
        FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://www.site2.com/images/" + target);
        req.UseBinary = true;
        req.Method = WebRequestMethods.Ftp.UploadFile;
        req.Credentials = new NetworkCredential("myUser", "myPass");
        StreamReader rdr = new StreamReader(image);
        byte[] fileData = Encoding.UTF8.GetBytes(rdr.ReadToEnd());

        rdr.Close();
        req.ContentLength = fileData.Length;
        Stream reqStream = req.GetRequestStream();
        reqStream.Write(fileData, 0, fileData.Length);
        reqStream.Close();
    }

This is where the method is called from (image1 is an HttpPostedFileBase):

myObject.UpLoadImage(image1.InputStream, storedTL.ID + "-1.png");

If there is a way that I can make this code work, or if there is an all around better way to do this, please help.

Isochronous answered 12/9, 2012 at 2:23 Comment(1)
can you check if images are uploaded created successfullyBeery
E
11

StreamReader is meant to read text.

Change:

StreamReader rdr = new StreamReader(image);
byte[] fileData = Encoding.UTF8.GetBytes(rdr.ReadToEnd());

to

byte[] fileData = File.ReadAllBytes(image);
Etsukoetta answered 12/9, 2012 at 2:28 Comment(2)
What's awesome about this is the OP got his code from the sample on the MSDN site msdn.microsoft.com/en-us/library/ms229715(v=vs.110).aspx. I know this because I did too and ended up here with (presumably) the same problem. The image files never open, they must get mangled by the SteamReader never to be usable again... :( Nowhere does it say that this method only works for text files. That might help an FTP through code newbie like myself if it did.Disassociate
What am I missing with this answer? The image variable defined in the OP's method is a Stream object, but File.ReadAllBytes takes a String input type.Triturable
S
0

Old question, but I lost some time with this issue today, and using ReadAllBytes was not an option to me, so I had to deal with Streams only. After some researching here is how I reach my solution:

    public void UploadToFTP(Stream stream, string ftpPath)
    {
        Stream requestStream = null;

        try
        {

            Uri uri = GetServerUri(ftpPath);

            FtpWebRequest request = Connect(uri); //here I set user/pwd/etc
            request.UseBinary = true;
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.ContentLength = stream.Length;

            requestStream = request.GetRequestStream();

            //Avoid to write zero length files in destiny. 
            //If you have read the stream before for any reason (as a convertion to Bitmap to extract dimensions, for example)
            stream.Seek(0, SeekOrigin.Begin);

            stream.CopyTo(requestStream);

        }
        catch (WebException ex)
        {
            //do something
        }
        finally
        {
            if (requestStream != null)
                requestStream.Close();

        }
    }
Schulte answered 21/9, 2016 at 1:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.