[英]Upload xml file on ftp binary
我有代碼,我將xml文件發送到ftp服務器,但是ftp服務器上的文件大小小於原始文件。 我正在嘗試啟用二進制傳輸,但結果仍然相同。
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();
}
原始文件的大小為294 672,但ftp The xml file on ftp is valid....But when i compare files in total comander, the original file have: FF FE 3C 00 3F 00.....and the file on ftp have 3C 00 3F 00...
但是文件的內容還可以...:/您知道嗎?
服務器上的XML文件有效嗎? 從代碼中,您正在使用Unicode讀取文件。 使用unicode編碼的文件通常具有一個字符,該字符位於文件的開頭,稱為字節順序標記 。 這可能是您在轉換過程中丟失2字節差異的原因。
更新通過Encoding.GetPreamble()
給出任何編碼的正確字節順序標記。
StreamReader sourceStream = new StreamReader(@"C:\\\\Users\\\\L\\\\Desktop\\\\data.xml"); //Get Preamble and 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 into 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(); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.