【发布时间】:2015-12-21 20:05:11
【问题描述】:
我正在尝试使用 C# 将 jpeg 上传到 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 流的字节数组中。只是想不出如何解决它。 任何帮助表示赞赏:-) 谢谢
【问题讨论】:
标签: c# ftp httppostedfilebase