所以我试图将文件上传到我的 ftp 服务器。每件事似乎都按预期工作,但是当我从 ftp 打开文件时,我收到 I/O 错误。本地文件工作得很好。上传后文件如何损坏。我在这里发现了类似的问题。
在这里,我读到您必须将传输模式更改为二进制。我尝试设置ftpRequest.UseBinary = true;
但我仍然收到 I/O 错误。我是否必须在其他地方更改传输模式?
这是我的ftp上传代码:
public string upload(string remoteFile, string localFile)
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
ftpRequest.UseBinary = true;
ftpRequest.Credentials = new NetworkCredential(user, pass);
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(localFile);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
ftpRequest.ContentLength = fileContents.Length;
Stream requestStream = ftpRequest.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
response.Close();
return string.Format("Upload File Complete, status {0}", response.StatusDescription);
}
使用 webclient 我得到错误:
远程服务器返回错误:(553) 文件名不允许。
这是我的代码:
private void uploadToPDF(int fileName, string localFilePath, string ftpPath, string baseAddress)
{
WebClient webclient = new WebClient();
webclient.BaseAddress = baseAddress;
webclient.Credentials = new NetworkCredential(username, password);
webclient.UploadFile(ftpPath + fileName + ".pdf", localFilePath);
}