ftp client - Saving base64String as image on FTP server, saves corrupted file -


saving base64string image on ftp server saving corrupted file. doing following things

  1. converted base64string byte[].
  2. initialized memorystream byte converted in above step.
  3. opened stream ftp
  4. write stream on ftp.

below code

public bool writefrombase64tofile(string base64, string path, string filename)     {         bool result = false;          using (ftpclient ftp = new ftpclient())         {              // setting ftp properties required values.             ftp.readtimeout = 999999999;             ftp.host = host;             ftp.credentials = new system.net.networkcredential(username, password);             ftp.port = convert.toint32(port);             ftp.dataconnectiontype = ftpdataconnectiontype.autopassive;             ftp.connect();             ftp.connecttimeout = 1000000;              // converting base64string byte array.             byte[] file = convert.frombase64string(base64);                if (ftp.isconnected)             {                 int buffer_size = file.length; // 64kb buffer                 byte[] buffer = new byte[file.length];                  // initializing memorystream byte converted base64string.                 memorystream ms = new memorystream(buffer);                  using (stream readstream = ms)                 {                     filename = filename.replacingspecialcharacterswithentities();                      // getting stream ftp , writing on ftp server.                     using (stream writestream = ftp.openwrite(path + "/" + filename+".jpg", ftpdatatype.binary))                     {                          while (readstream.position < readstream.length)                         {                             buffer.initialize();                              // reading stream                             int bytesread = readstream.read(buffer, 0, buffer_size);                             // writing stream                             writestream.write(buffer, 0, bytesread);                         }                          // flushing stream.                         writestream.flush();                     }                 }             }         }         result = true;         return result;     } 


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -