user1237131
user1237131

Reputation: 1863

how to save image in ftp server in c#?

how to save stream data as image in ftp server?

 FileInfo fileInf = new FileInfo("1" + ".jpg");
                        string uri = "ftp://" + "hostip//Data//" + fileInf.Name;
                        FtpWebRequest reqFTP;

                        // Create FtpWebRequest object from the Uri provided
                        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(
                             "ftp://" + "ipaddress//Data//" + fileInf.Name));

                        // Provide the WebPermission Credintials
                        reqFTP.Credentials = new NetworkCredential("username",
                                                              "password");

                        // By default KeepAlive is true, where the control connection is 
                        // not closed after a command is executed.
                        reqFTP.KeepAlive = false;

                        // Specify the command to be executed.
                        reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

                        // Specify the data transfer type.
                        reqFTP.UseBinary = true;

                        // Notify the server about the size of the uploaded file
                        //reqFTP.ContentLength = fileInf.Length; ???
                        using (var img = Image.FromStream(image))
                        {
                            img.Save(adduser.User_Id + ".jpg", ImageFormat.Jpeg);
                        }

can u please tell me.

Upvotes: 0

Views: 2867

Answers (2)

Jayesh Sorathia
Jayesh Sorathia

Reputation: 1614

Here are sample code for Download file from FTP Server

Uri url = new Uri("ftp://ftp.demo.com/Image1.jpg");
if (url.Scheme == Uri.UriSchemeFtp)
{
    FtpWebRequest objRequest = (FtpWebRequest)FtpWebRequest.Create(url);
    //Set credentials if required else comment this Credential code
    NetworkCredential objCredential = new NetworkCredential("FTPUserName", "FTPPassword");
    objRequest.Credentials = objCredential;
    objRequest.Method = WebRequestMethods.Ftp.DownloadFile;
    FtpWebResponse objResponse = (FtpWebResponse)objRequest.GetResponse();
    StreamReader objReader = new StreamReader(objResponse.GetResponseStream());
    byte[] buffer = new byte[16 * 1024];
    int len = 0;
    FileStream objFS = new FileStream(Server.MapPath("Image1.jpg"), FileMode.Create, FileAccess.Write, FileShare.Read);
    while ((len = objReader.BaseStream.Read(buffer, 0, buffer.Length)) != 0)
    {
        objFS.Write(buffer, 0, len);
    }
    objFS.Close();
    objResponse.Close();
}

Upvotes: 0

Jim Mischel
Jim Mischel

Reputation: 134035

You need to get the data (the image) into a byte array and then send that. The FtpWebRequest.GetResponse documentation example shows the basics, although it's appending a file. Everything else is relevant to what you're doing (you'd replace the append with upload file).

To get the image into a byte array, you can write:

byte[] imageBuffer = File.ReadAllBytes(imageFileName);

Everything else should be very similar to the documentation example.

Upvotes: 1

Related Questions