ftp client - Saving base64String as image on FTP server, saves corrupted file -
saving base64string image on ftp server saving corrupted file. doing following things
- converted base64string byte[].
- initialized memorystream byte converted in above step.
- opened stream ftp
- 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
Post a Comment