2015-12-21 97 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; 
     } 
    } 
}