C# 上载.pdf文件到ftp服务器时出现问题-.pdf已创建,但为空
这是我的密码C# 上载.pdf文件到ftp服务器时出现问题-.pdf已创建,但为空,c#,ftp,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 fi
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文件不正确。我不知道问题出在哪里 既然Microsoft已经实现了FTP上传文件,为什么还要重新发明FTP上传文件 “STOR”表示这是FTP中的上载
将像PDF这样的二进制文件作为字符串读入内存真的安全吗?那么您建议采用什么方法?file.ReadAllBytes(文件路径)获取文件的字节[],然后将字节[]传递给sw.write(),这就是我一直在寻找的!谢谢,不客气。很高兴帮助你。
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential("log", "pass");
client.UploadFile("your_server", "STOR", filePath);
}