即時嘗試使用C#將jpeg上傳到ftp服務器。該文件已上傳,但打開時已損壞。 這是我的代碼:使用C#FTP上傳時圖像損壞
/// <summary>
/// Upload HttpPostedFileBase to ftp server
/// </summary>
/// <param name="image">type of HttpPostedFileBase</param>
/// <param name="targetpath">folders path in ftp server</param>
/// <param name="source">jpeg image name</param>
public static void UploadFileToFTP(HttpPostedFileBase image,string targetpath,string source)
{
string ftpurl = ConfigurationManager.AppSettings["Ftp_Images_Domain"];
string ftpusername = ConfigurationManager.AppSettings["Ftp_Images_Usr"];
string ftppassword = ConfigurationManager.AppSettings["Ftp_Images_Pwd"];
try
{
SetMethodRequiresCWD();
string filename = Path.GetFileName(source);
string ftpfullpath = ftpurl + targetpath + source;
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);
ftp.KeepAlive = false;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
HttpPostedFileBase myFile = image;
int nFileLen = myFile.ContentLength;
byte[] myData = new byte[nFileLen];
Stream ftpstream = ftp.GetRequestStream();
ftpstream.Write(myData, 0, nFileLen);
ftpstream.Close();
ftpstream.Flush();
}
catch (WebException e)
{
String status = ((FtpWebResponse)e.Response).StatusDescription;
}
}
我認爲問題是字節數組我寫的FTP流。只是不知道如何解決它。 任何幫助表示讚賞:-)謝謝
我可以看到我的錯誤。它工作感謝你:) – Basilf
我今天來到這裏,當我閱讀有關CopyTo方法時,我想 - 「當然...」_謝謝。你也可以通過在'使用...'之前添加'image.Seek(0,SeekOrigin.Begin)'來避免零長文件; – Alexandre