所以我试图上传一个文件到我的FTP服务器。每件事似乎都像预期的那样工作,但是当我从ftp打开文件时,我收到一个I/O错误。本地文件工作得很好。上传后的一些文件如何损坏。我发现了一个类似的问题here。上传到FTP后打开pdf的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我得到的错误:
The remote server returned an error: (553) File name not allowed.
这里是我的代码:
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);
}
您是否看到该问题? http://stackoverflow.com/a/9738609/1346943 –
@ledbutter当我使用webclient上传文件时,我收到消息“远程服务器返回错误:(553)文件名不允许。” –
什么是文件名? – Tony