使用C#将txt文件上传到FTP服务器时的额外数据
好,所以我正在尝试通过尝试上载具有"HELLO WORLD"内容的文本文件来测试将文件上载到FTP服务器. 我得到的回报是 上传文件已完成System.Net.WebClient错误->已取消-> False". 该文件似乎出现在服务器上,但是当我打开它时,内容显示为:
Ok, so I am testing uploading files to an FTP server by attempting to upload a text file with the contents of "HELLO WORLD". I am given a return of" "upload file completed System.Net.WebClient error -> cancelled ->False". The file seems to appear on the server but when I open it, the contents read:
--------------8d30e4d69803578
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: application/octet-stream
HELLO WORLD
--------------8d30e4d69803578
我正在使用的代码是:
string ftpUserName = "ftpUserName";
string ftpPassword = "ftpPassword";
string ftpURL = "ftp://ftpServer.com/text.txt";
string path = "pathToFile/test.txt"
public static void Test()
{
System.Uri uri = new System.Uri(ftpURL);
FileInfo file = new FileInfo(path);
if (!file.Exists)
{
return;
}
using(WebClient wc = new WebClient())
{
wc.Credentials = new NetworkCredential(ftpUserName,ftpPassword);
wc.UploadFileCompleted += UploadFileCompleted;
wc.UploadFileAsync(uri,"STOR",path);
}
}
任何帮助将不胜感激
修改
我也刚刚尝试了一个zip文件,它已损坏. .txt和.zip都在到达服务器后也都变得更小,因此我认为由于该错误导致上传失败
I also just tried with a zip file and it is corrupt. Both the .txt and .zip are also far smaller once they reach the server, so I am assuming the upload has failed because of that error
修改2
使用.net2.0版本的FtpWebRequest
solved it using .net2.0's version of the FtpWebRequest
我刚刚验证了-除了我提到的异步问题,您的代码没有任何问题.
I have just verified - other than the async issue I mentioned, there's nothing wrong with your code.