所以我試圖上傳一個文件到我的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