c# - Upload xml file on ftp binary -
i have code, send xml file ftp server, size of file on ftp server smaller original file. i'm trying enable binary transmission, result still same.
fileinfo f = new fileinfo("c:\\users\\l\\desktop\\data.xml"); long original_vel = f.length; ftpwebrequest request = (ftpwebrequest)webrequest.create("ftp://***"); request.usebinary = true; request.method = webrequestmethods.ftp.getfilesize; request.method = webrequestmethods.ftp.uploadfile; request.credentials = new networkcredential("*****", "*****"); streamreader sourcestream = new streamreader(@"c:\\users\\l\\desktop\\data.xml"); byte[] filecontents = encoding.unicode.getbytes(sourcestream.readtoend()); sourcestream.close(); request.contentlength = filecontents.length; long ftp_vel = request.contentlength; stream requeststream = request.getrequeststream(); requeststream.write(filecontents, 0, filecontents.length); requeststream.close(); ftpwebresponse response = (ftpwebresponse)request.getresponse(); if (original_vel == ftp_vel) { response.close(); } else { odesilani(); }
the size of original file 294 672, file on ftp have 294 670. the xml file on ftp valid....but when compare files in total comander, original file have: ff fe 3c 00 3f 00.....and file on ftp have 3c 00 3f 00...
but content of file ok...:/ have idea?
is xml file @ server valid? code, reading file using unicode. files encoded using unicode have character placed @ beginning of file called byte order mark . may reason have 2-byte difference lost during conversion.
update proper byte order mark encoding given encoding.getpreamble()
fix code above be..
streamreader sourcestream = new streamreader(@"c:\\users\\l\\desktop\\data.xml"); //get preamble , file contents byte[] bom = encoding.unicode.getpreamble(); byte[] content = encoding.unicode.getbytes(sourcestream.readtoend()); //create destination array byte[] filecontents = new byte[bom.length + content.length]; //copy arrays destination appending bom if available array.copy(bom, 0, filecontents, 0, bom.length); array.copy(content, 0, filecontents, bom.length, content.length); request.contentlength = filecontents.length; long ftp_vel = request.contentlength; stream requeststream = request.getrequeststream(); requeststream.write(filecontents, 0, filecontents.length); requeststream.close(); ftpwebresponse response = (ftpwebresponse)request.getresponse(); if (original_vel == ftp_vel) { response.close(); } else { odesilani(); }
Comments
Post a Comment