2012-12-13 2 views
0

FTP 및 asp.net을 처음 사용합니다. 로컬 호스트 테스트 중에 만 코드가 작동하지만 go-daddy에서 라이브 테스트 중에 오류가 발생합니다. 어떤 도움을 주셔서 감사합니다.이미지 폴더에 ftp 이미지 업로드는 로컬 호스트에서만 작동하며 라이브 테스트 중에 오류가 발생합니다.

나는 현재이 웹 사이트의 모든 페이지와 코드를 호스팅하고있어이 AMUS 폴더

Unable to connect to the remote server 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Net.WebException: Unable to connect to the remote server 

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace: 


[WebException: Unable to connect to the remote server] 
    System.Net.FtpWebRequest.GetRequestStream() +839 
    DBMiddleTier.addImageFTP(FileUpload file) +360 
    Admin_CrudOperation.imgAddProduct_Click(Object sender, ImageClickEventArgs e) +96 
    System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e) +115 
    System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +120 
    System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563 

내 코드에

//method to add image ftp 
    public string addImageFTP(FileUpload file) 
    { 
     string filename = Path.GetFileName(file.FileName); 
     Bitmap src = Bitmap.FromStream(file.PostedFile.InputStream) as Bitmap; 
     Bitmap result = new Bitmap(src, 300, 300); 
     string saveName = savePath + filename; 
     result.Save(saveName, ImageFormat.Jpeg); 
     System.Net.FtpWebRequest request = System.Net.WebRequest.Create("ftp://***.***.***.*/AMUS/images/" + filename) as System.Net.FtpWebRequest; 
     //this example assumes the FTP site uses anoymous login on 
     //NetWorkCredentials provides credentials for password-based authentication such as digest, basic, NTLM 
     request.Credentials = new System.Net.NetworkCredential("Username", "Password"); 

     //Copy the contents of the file to the request stream 
     byte[] fileContents = null; 
     if (file.HasFile) 
     { 
      //fileContents = FileUploadControl.FileBytes; 
      fileContents = File.ReadAllBytes(saveName); 

     } 
     else 
     { 
      string res = "you need to provide a file"; 
      return res; 
     } 
     request.Method = System.Net.WebRequestMethods.Ftp.UploadFile; 
     request.ContentLength = fileContents.Length; 
     //GetReequestStream: retrieves the stream used to upload data to an FTP server. 
     Stream requestStream = request.GetRequestStream(); 
     requestStream.Write(fileContents, 0, fileContents.Length); 
     requestStream.Close(); 
     return "Successful Upload"; 
    } 

답변

0
/// <summary> 
    /// ftpClient.upload("etc/test.txt", @"C:\Users\metastruct\Desktop\test.txt"); 
    /// </summary> 
    /// <param name="remoteFile"></param> 
    /// <param name="localFile"></param> 
    public void uploadFile(string remoteFile, string localFile) 
    { 
     try 
     { 
      /* Create an FTP Request */ 
      ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile); 
      /* Log in to the FTP Server with the User Name and Password Provided */ 
      ftpRequest.Credentials = new NetworkCredential(user, pass); 
      /* When in doubt, use these options */ 
      ftpRequest.UseBinary = true; 
      ftpRequest.UsePassive = true; 
      ftpRequest.KeepAlive = true; 
      /* Specify the Type of FTP Request */ 
      ftpRequest.Method = WebRequestMethods.Ftp.UploadFile; 
      /* Establish Return Communication with the FTP Server */ 
      ftpStream = ftpRequest.GetRequestStream(); 
      /* Open a File Stream to Read the File for Upload */ 
      FileStream localFileStream = new FileStream(localFile, FileMode.Create); 
      /* Buffer for the Downloaded Data */ 
      byte[] byteBuffer = new byte[bufferSize]; 
      int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize); 
      /* Upload the File by Sending the Buffered Data Until the Transfer is Complete */ 
      try 
      { 
       while (bytesSent != 0) 
       { 
        ftpStream.Write(byteBuffer, 0, bytesSent); 
        bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize); 
       } 
      } 
      catch (Exception ex) { Console.WriteLine(ex.ToString()); } 
      /* Resource Cleanup */ 
      localFileStream.Close(); 
      ftpStream.Close(); 
      ftpRequest = null; 
     } 
     catch (Exception ex) { Console.WriteLine(ex.ToString()); } 
     return; 
    } 

/// <summary> 
    /// ftp.uploadFolderContents("httpdocs/omid", @"E:\omid"); 
    /// </summary> 
    /// <param name="remoteFolder"></param> 
    /// <param name="localFolder"></param> 
    /// <returns></returns> 
    public bool uploadFolderContents(string remoteFolder, string localFolder) 
    { 
     bool rslt = false; 

     try 
     { 
      string[] files = Directory.GetFiles(localFolder); 
      string[] dirs = Directory.GetDirectories(localFolder); 
      foreach (string file in files) 
      { 
       uploadFile(remoteFolder + "/" + Path.GetFileName(file), file); 
      } 
      foreach (string dir in dirs) 
      { 
       createFolder(remoteFolder + "/" + Path.GetFileName(dir)); 
       uploadFolderContents(remoteFolder + "/" + Path.GetFileName(dir), dir); 
      } 

      rslt = true; 
     } 
     catch (Exception) 
     { 
      rslt = false; 
     } 

     return rslt; 
    } 

     /// <summary> 
    /// ftpClient.createDirectory("etc/test"); 
    /// </summary> 
    /// <param name="newDirectory"></param> 
    public void createFolder(string newDirectory) 
    { 
     try 
     { 
      /* Create an FTP Request */ 
      ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + newDirectory); 
      /* Log in to the FTP Server with the User Name and Password Provided */ 
      ftpRequest.Credentials = new NetworkCredential(user, pass); 
      /* When in doubt, use these options */ 
      ftpRequest.UseBinary = true; 
      ftpRequest.UsePassive = true; 
      ftpRequest.KeepAlive = true; 
      /* Specify the Type of FTP Request */ 
      ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory; 
      /* Establish Return Communication with the FTP Server */ 
      ftpResponse = (FtpWebResponse)ftpRequest.GetResponse(); 
      /* Resource Cleanup */ 
      ftpResponse.Close(); 
      ftpRequest = null; 
     } 
     catch (Exception ex) { Console.WriteLine(ex.ToString()); } 
     return; 
    } 
관련 문제