2012-05-15 109 views
0

請幫幫我,我有一個很大的問題。上傳ftp服務器上的文件時出錯

我想上傳一個使用asp.net c#的godaddy ftp服務器上的文件。當我在Visual Studio中運行應用程序時,該文件在ftp服務器上成功創建,但是當我使用諸如(www.domain/page.aspx)之類的URL創建此文件時,我得到以下錯誤(使用asp.net 4.0):

無法連接到遠程服務器

當我使用asp.net 3.5我得到這個錯誤:

請求類型的權限「System.Net.WebPermission ,System,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'失敗。

請幫幫我。

+0

我使用此代碼上傳文件//的FtpWebRequest FTPReq1 =(的FtpWebRequest)FtpWebRequest.Create( 「ftp://domain.com/」 + FileUpload1.FileName); //FTPReq1.UseBinary = true; //FTPReq1.Credentials = new NetworkCredential(「username」,「pass」); //FTPReq1.Method = WebRequestMethods.Ftp.UploadFile; // FtpWebResponse fres1 =(FtpWebResponse)FTPReq1.GetResponse(); –

回答

1

確保您正在使用有權訪問FTP的憑據。此外,您的網址可能類似ftp://www.domain.com/

以下是我過去使用的一些FTP代碼。

public class FTP 
{ 
    private String Username { get; set; } 
    private String Password { get; set; } 
    private String Host { get; set; } 
    private Int32 Port { get; set; } 

    public FTP(String username, String password, String host, Int32 port) 
    { 
     Username = username; 
     Password = password; 
     Host = host; 
     Port = port; 
    } 

    private Uri BuildServerUri(string Path) 
    { 
     return new Uri(String.Format("ftp://{0}:{1}/{2}", Host, Port, Path)); 
    } 

    /// <summary> 
    /// Upload a byte[] to the FTP server 
    /// </summary> 
    /// <param name="path">Path on the FTP server (upload/myfile.txt)</param> 
    /// <param name="Data">A byte[] containing the data to upload</param> 
    /// <returns>The server response in a byte[]</returns> 
    private byte[] UploadData(string path, byte[] Data) 
    { 
     // Get the object used to communicate with the server. 
     WebClient request = new WebClient(); 

     try 
     { 
      // Logon to the server using username + password 
      request.Credentials = new NetworkCredential(Username, Password); 
      return request.UploadData(BuildServerUri(path), Data); 
     } 
     finally 
     { 
      if (request != null) 
       request.Dispose(); 
     } 
    } 

    /// <summary> 
    /// Load a file from disk and upload it to the FTP server 
    /// </summary> 
    /// <param name="ftppath">Path on the FTP server (/upload/myfile.txt)</param> 
    /// <param name="srcfile">File on the local harddisk to upload</param> 
    /// <returns>The server response in a byte[]</returns> 
    public byte[] UploadFile(string ftppath, string srcfile) 
    { 
     // Read the data from disk 
     FileStream fs = new FileStream(srcfile, FileMode.Open); 
     try 
     { 
      byte[] FileData = new byte[fs.Length]; 

      int numBytesToRead = (int)fs.Length; 
      int numBytesRead = 0; 
      while (numBytesToRead > 0) 
      { 
       // Read may return anything from 0 to numBytesToRead. 
       int n = fs.Read(FileData, numBytesRead, numBytesToRead); 

       // Break when the end of the file is reached. 
       if (n == 0) break; 

       numBytesRead += n; 
       numBytesToRead -= n; 
      } 

      numBytesToRead = FileData.Length; 

      // Upload the data from the buffer 
      return UploadData(ftppath, FileData); 
     } 
     finally 
     { 
      if (fs != null) 
       fs.Close(); 
      if (fs != null) 
       fs.Dispose(); 
     } 
    } 
} 
+0

感謝您的幫助,但我得到了同樣的錯誤(無法連接到遠程服務器) –

+0

您能通過命令行連接並傳輸文件嗎? http://kb2.adobe.com/cps/164/tn_16418.html – Mark

+0

我知道命令行,但我的項目需要使用從url請求的asp.net創建文件和文件夾,感謝您的幫助。 –