2013-05-02 2 views
1

프로덕션 서버에서 ftp 파일을 만드는 데 문제가 있습니다. 다음 코드는 내 개발 서버에서 잘 작동하지만, 나는이 사이트에 프로덕션 서버에 내 코드를 이동할 때 나는이 오류를ftp asp.net 파일을 업로드 할 수 없습니다

코드를 제공 호스팅 구입 한 곳

string fullname = Convert.ToString(TxtBx_FullName.Text); 

if (FU_Img.HasFile) 
{ 
    string ftpUrl = ""; 

    HttpPostedFile userPostFile = FU_Img.PostedFile; 
    string uploadUrl = @"ftp://abc/upload/u" + fullname; 
    string uploadFileName = Path.GetFileName(FU_Img.FileName); 
    uploadFileName = "image." + uploadFileName.Split('.')[1]; 


    Stream streamObj = userPostFile.InputStream; 
    Byte[] buffer = new Byte[userPostFile.ContentLength]; 
    streamObj.Read(buffer, 0, buffer.Length); 
    streamObj.Close(); 
    streamObj = null; 
    // FtpWebResponse CreateForderResponse; 
    if (!GetAllFilesList(uploadUrl, "xyz", "******")) 
    { 
     FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(uploadUrl); 
     ftp.Method = WebRequestMethods.Ftp.MakeDirectory; 
     ftp.Credentials = new NetworkCredential("xyz", "******"); 
     FtpWebResponse response = (FtpWebResponse)ftp.GetResponse(); 
    } 
    //if (CreateForderResponse.StatusCode == FtpStatusCode.PathnameCreated) 


    ftpUrl = string.Format("{0}/{1}", uploadUrl, uploadFileName); 

    FtpWebRequest requestObj = FtpWebRequest.Create(ftpUrl) as FtpWebRequest; 
    requestObj.KeepAlive = false; 
    requestObj.UseBinary = true; 
    requestObj.Method = WebRequestMethods.Ftp.UploadFile; 
    requestObj.Credentials = new NetworkCredential("xxxx", "xxxxxx"); 

    Stream requestStream = requestObj.GetRequestStream(); <----- error 
    requestStream.Write(buffer, 0, buffer.Length); 
    requestStream.Flush(); 
    requestStream.Close(); 
    requestObj = null; 

오류

스택 추적

[WebException: The remote server returned an error: (550) File unavailable (e.g., file not found, no access).] 
    System.Net.FtpWebRequest.SyncRequestCallback(Object obj) +341 
    System.Net.FtpWebRequest.RequestCallback(Object obj) +23 
    System.Net.CommandStream.Dispose(Boolean disposing) +19 
    System.IO.Stream.Close() +20 
    System.IO.Stream.Dispose() +10 
    System.Net.ConnectionPool.Destroy(PooledStream pooledStream) +188 
    System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Boolean canReuse) +118 
    System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage) +655 
    System.Net.FtpWebRequest.GetRequestStream() +795 
    MentorMentee.SignUp.signup.Btn_Preview_Click(Object sender, EventArgs e) +954 
    System.EventHandler.Invoke(Object sender, EventArgs e) +0 
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9553178 
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103 
    System.Web.UI.WebControls.Button.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) +35 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724 

KeepAlive 속성을 false로 설정했지만 작동하지 않았습니다. 내가 어디로 잘못 가고 있니?

+1

http://stackoverflow.com/questions/838584/upload-to-ftp-asp-net – Freelancer

+0

글쎄, 모든 노력에도 불구하고 Kevin은 여전히 ​​자신의 암호를 변경해야합니다 :) –

+0

' ftpUrl'과'uploadUrl' – Pete

답변

0

다음 해결책은 나를 위해 해결되었습니다.

private bool FtpUpload(FileUpload file, string ftpServer, string username, string ftpPass, string domainName = "") 
    { 
     // ftp://domain\user:[email protected]/url-path 
     // If you are a member of a domain, then "ftp://domain-name\username:[email protected]" may fail because the backslash (\) is sent in as a literal character and Internet Explorer incorrectly looks for a file instead of parsing a Web address. Changing the backslash (\) in the domain-name\username to domainname%5Cusername works correctly. 

     try 
     { 
      string ftpAddres; 
      if (domainName != string.Empty) 
       ftpAddres = "ftp://" + domainName + @"%5C" + username + ":" + ftpPass + "@" + ftpServer + "/" + file.FileName; 
      else 
       ftpAddres = "ftp://" + username + ":" + ftpPass + "@" + ftpServer + "/" + file.FileName; 

      using (var webClient = new System.Net.WebClient()) 
      { 
       webClient.UploadData(new Uri(ftpAddres), file.FileBytes); 
      } 

     } 
     catch (Exception e) 
     { 
      throw new Exception(e.Message, e); 
     } 
     return true; 
    } 

@Freelancer에게 귀하가 코멘트에 올린 링크를 보내 주셔서 감사합니다.

관련 문제