Shumii
Shumii

Reputation: 4581

How to FTP an Image using C# in .NET

I have an Image in memory and would like to convert that into a stream so I can FTP. I have done just that using a MemoryStream and the FtpWebRequest. However, the result is a file which cannot be opened.

A scenario that does work is when I first save the Image to a physical file. Then I use a FileStream to open that physical file and send that stream for Ftp.

I don't want to have to physically save it first for various reasons - is it possible to do this without saving it?

Upvotes: 0

Views: 3934

Answers (3)

Peter Holmes
Peter Holmes

Reputation: 652

Without seeing your code, it is hard to workout what the problem is. As it works when you open a new stream, i.e. from a file, my best guess is that the problem is related to the way you are handling the stream. Make sure that when you've finished writing to the stream you set its position back to the start, using either the Seek method or the Position property.

Upvotes: 2

kmatyaszek
kmatyaszek

Reputation: 19296

Try this:

class FTPHelper
{
    public FTPHelper(string address, string login, string password)
    {
        Address = address;
        Login = login;
        Password = password;
    }

    public void Upload(MemoryStream stream, string fileName)
    {
        try
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(Address + @"/" + fileName);
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential(Login,Password);

            request.UseBinary = true;

            byte[] buffer = new byte[stream.Length];
            stream.Read(buffer, 0, buffer.Length);
            stream.Close();

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(buffer, 0, buffer.Length);
            requestStream.Close();

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
            response.Close();
        }
        catch (Exception)
        {
            throw;
        }
    }

    public string Address { get; set; }
    public string Login { get; set; }
    public string Password { get; set; }
}

Usage of the FTPHelper class:

byte[] data;
using (Image image = Image.FromFile(@"C:\test\test.jpg"))
{
    using (MemoryStream m = new MemoryStream())
    {
        image.Save(m, image.RawFormat);
        data = m.ToArray();
    }
}

FTPHelper ftpHelper = new FTPHelper("ftp://localhost", "test", "test");
ftpHelper.Upload(new MemoryStream(data), "test.jpeg");

Upvotes: 0

varun
varun

Reputation: 4650

You need to post your code for anyone to answer more accurately.

In general, if the out put is getting corrupt, you may want to check params like

ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;

Well here is a brilliyant article in how to do so,

I would suggest testing by first ftping a proper image to desired location (to test successful transfer), and also testing the code which dumps the image to a file (to see if all is good there).

Upvotes: 0

Related Questions