2014-02-26 3 views
1

이 코드의 문제점은 무엇입니까? 임 000webhost에 FTP를 호스팅하고 난 내 프로그램의 사용자가 컴퓨터에서 열립니다 이미지를 엽니 다 OpenFileDialog를 기능FTP 업로드 이미지

에게 버튼을 사용하여 이미지를 업로드 할 :

 OpenFileDialog open = new OpenFileDialog(); 
     if (open.ShowDialog() == DialogResult.OK) 
     { 
      Bitmap bit = new Bitmap(open.FileName); 
      pictureBox1.Image = bit; 
      pictureBox2.Image = bit; 
      bit.Dispose(); 
      string fullPath = open.FileName; 
      string fileName = open.SafeFileName; 
      string path = fullPath.Replace(fileName, ""); 
      User.Details.UpLoadImage(fullPath); 
     } 

업로드하는 코드 : (530) 로그인하지 않음 : 나는 이 "요청 된 URI이 FTP 명령 무효"두 번째 오류가 일부 errrors가 계속

try 
     { 
      String sourcefilepath = source; // e.g. “d:/test.docx” 
      String ftpurl = "ftp://www.locu.site90.com/public_html/"; // e.g. ftp://serverip/foldername/foldername 
      String ftpusername = "********"; // e.g. username 
      String ftppassword = "********"; // e.g. password 
      string filename = Path.GetFileName(source); 
      string ftpfullpath = ftpurl; 
      FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath); 
      ftp.Credentials = new NetworkCredential(ftpusername, ftppassword); 

      ftp.KeepAlive = true; 
      ftp.UseBinary = true; 
      ftp.Method = WebRequestMethods.Ftp.UploadFile; 

      FileStream fs = File.OpenRead(source); 
      byte[] buffer = new byte[fs.Length]; 
      fs.Read(buffer, 0, buffer.Length); 
      fs.Close(); 

      Stream ftpstream = ftp.GetRequestStream(); 
      ftpstream.Write(buffer, 0, buffer.Length); 
      ftpstream.Close(); 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
    } 

는 "원격 서버에서 오류를 반환 . "

+0

아마 서버가 보안 연결 (SSL), ftp.EnableSSL이 필요 = true. – user2453734

+0

그리고 어떻게 해결할 수 있습니까? – user3354197

+0

부적절한 URL은 끝 부분에 파일 이름을 포함하도록 URL을 변경하여 해결할 수 있습니다. 예 : String ftpurl = "ftp://www.locu.site90.com/public_html/test.txt"; ftpfullpath = ftpurl + filename; 문자열을 사용하려고했을 수도 있습니다. – user2453734

답변

1

업로드를하고 있기 때문에. 대상 파일 이름은 FTP URL에 필요합니다.

string ftpfullpath = ftpurl; 

가로 변경합니다 : 그것은처럼 보이는 당신이 다음 줄로 할 의도하는의 수 무엇 오류에 기록되지 들어

string ftpfullpath = ftpurl + filename; 

, 일부 호스팅 회사는 보안 연결 만 허용 . 당신은 당신의 코드에 다음 줄을 추가 시도 할 수 있습니다 :

ftp.EnableSsl = true; 
+0

안녕하세요. 첫 번째 오류가 수정 된 코드를 작성했습니다. 이제 두 번째 오류를 수신하고 있습니다 - 처리되지 않은 'System.Net.WebException'형식의 예외가 App.exe에서 발생했습니다. 추가 정보 : 원격 서버에서 오류를 반환했습니다 : (530) 로그인하지 않았습니다. – user3354197

+0

자격 증명이 맞습니까? 내가 filezilla와 같은 것을 사용하고 도구를 통해 먼저 연결할 수 있는지 확인하십시오. 그런 다음 한 번 작동합니다. 그런 다음 코딩 해보십시오. – user2453734

+0

나는 당신의 enableSsl 수정을했고 그것을 수정했습니다. 왜 내가이 오류를 내게 던지고 있는지 당신이 생각해? - App.exe에서 'System.Net.WebException'유형의 처리되지 않은 예외가 발생했습니다. 추가 정보 : 원격 서버에서 오류를 반환했습니다 : (500) 구문 오류, 명령을 인식 할 수 없습니다. – user3354197

0

나는이 방법을 사용하고, 그 잘 작동 :

public static void UpLoadImage(string image, string targeturl) 
     { 
      FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://www.website.com/images/" + targeturl); 
      req.UseBinary = true; 
      req.Method = WebRequestMethods.Ftp.UploadFile; 
      req.Credentials = new NetworkCredential("user", "pass"); 
      byte[] fileData = File.ReadAllBytes(image); 
      req.ContentLength = fileData.Length; 
      Stream reqStream = req.GetRequestStream(); 
      reqStream.Write(fileData, 0, fileData.Length); 
      reqStream.Close(); 
     }