2015-12-21 50 views
0

即时尝试使用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流。只是不知道如何解决它。 任何帮助表示赞赏:-)谢谢

回答

3

byte[] myData永远不会与图像数据初始化。以下是正确的代码。我从代码中删除了一些不需要的行。试试吧,让我知道,如果它工作得很好或不

/// <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 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; 

     using (Stream ftpstream = ftp.GetRequestStream()) 
      image.InputStream.CopyTo(ftpstream) 
    } catch (WebException e) { 
     String status = ((FtpWebResponse)e.Response).StatusDescription; 
    } 
} 
+0

我可以看到我的错误。它工作感谢你:) – Basilf

+0

我今天来到这里,当我阅读有关CopyTo方法时,我想 - “当然...”_谢谢。你也可以通过在'使用...'之前添加'image.Seek(0,SeekOrigin.Begin)'来避免零长文件; – Alexandre

0

我没有HttpPostedFileBase但下面的方法作品尝试一下,应该给你什么在代码中发生的事情的想法。

public class Test 
{ 
    public static void UploadFileToFTP() 
    { 

     string ftpurl = "ftp://ServerName:21/test.txt"; 

     try 
     { 
      string filename = "F:\\testing.txt"; 
      string ftpfullpath = ftpurl; 
      FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath); 
      ftp.Credentials = new NetworkCredential(); 

      //ftp.KeepAlive = false; 
      ftp.UsePassive = false; 
      ftp.Method = WebRequestMethods.Ftp.UploadFile; 
      byte[] myFile = File.ReadAllBytes(filename); 
      ftp.ContentLength = myFile.Length; 
      Stream ftpstream = ftp.GetRequestStream(); 
      ftpstream.Write(myFile, 0, myFile.Length); 
      ftpstream.Close(); 
      ftpstream.Flush(); 
     } 
     catch (WebException e) 
     { 
      String status = ((FtpWebResponse)e.Response).StatusDescription; 
     } 
    } 
}