Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 上载.pdf文件到ftp服务器时出现问题-.pdf已创建,但为空_C#_Ftp - Fatal编程技术网

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);
}