Uploading image to FTP mono for Android

0
uplaoding images to ftp mono for android

Uploading your pictures saved on the SD Card or taken from your mobile phone camera can be easily uploaded to FTP using mono for android. Below is a short code snipped which does exactly that.

You set the directory to

SDCard / Pictures / MyPics

Provide your FTP host, username and password and uploads the images in your uploades folder on the root on your FTP



 var dir = new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures), "MyPics");

string ftpHost     = "ftp.myhost.com";

string ftpUser     = "username";

string ftpPassword = "password";


if (upload)
{
	foreach (string picture in pictures)
	{
		if (picture != null)
		{
          string ftpfullpath = "ftp://" + ftphost + "/uploads/" + picture;
          
          FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
          
          //userid and password for the ftp server	

          ftp.Credentials = new NetworkCredential(ftpUser, ftpPassword);
          
          ftp.KeepAlive = true;
          ftp.UseBinary = true;
          ftp.Method = WebRequestMethods.Ftp.UploadFile;

          using (FileStream fs = File.OpenRead(dir + "//" + picture))
          {
            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
            fs.Close();
            
            Stream ftpstream = ftp.GetRequestStream();
            ftpstream.Write(buffer, 0, buffer.Length);
            ftpstream.Close();
          }
		}
	}
}


Let me know your thoughts in the comments below. Any questions i will be glad to help

Get Free Email Updates!

Signup now and receive free offers, discounts & coupon codes

I agree to have my personal information transfered to Mad Mimi ( more information )

I will never give away, trade or sell your email address. You can unsubscribe at any time.

Leave a Reply

Your email address will not be published. Required fields are marked *

CommentLuv badge

This site uses Akismet to reduce spam. Learn how your comment data is processed.