将.pdf文件上传到ftp服务器的问题-创建了.pdf但为空

时间:2018-07-18 11:19:11

标签: c# ftp

这是我的代码。

FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create("path");
ftpRequest.Credentials = new NetworkCredential("log", "pass");

ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
byte[] fileContent;  //in this array you'll store the file's content

using (StreamReader sr = new StreamReader(@"pathToFile"))  //'myFile.txt' is the file we want to upload
{
    fileContent = Encoding.UTF8.GetBytes(sr.ReadToEnd()); //getting the file's content, already transformed into a byte array
}

using (Stream sw = ftpRequest.GetRequestStream())
{
    sw.Write(fileContent, 0, fileContent.Length);  //sending the content to the FTP Server
}

.txt文件可以正确上传内容,而.pdf文件则不能。不知道问题出在哪里。

1 个答案:

答案 0 :(得分:1)

在Microsoft已实施WebClient的情况下,为什么要重新发明上传文件FTP?

  

“ STOR”表示这是FTP上传

using (WebClient client = new WebClient())
{
    client.Credentials = new NetworkCredential("log", "pass");
    client.UploadFile("your_server", "STOR", filePath);
}