2010-08-30 71 views
0

嘗試從ftp(有權訪問)下載項目時,得到「System.NotSupportedException:在System.Security.Util.StringExpressionSet不支持給定路徑的格式。上傳似乎很好地工作,並完成類似下面我將發佈兩個功能:c#asp.net FTP錯誤

private void Download(string filePath, string fileName) 
    { 
     FtpWebRequest reqFTP; 
     try 
     { 
      //filePath = <<The full path where the file is to be created. the>>, 
      //fileName = <<Name of the file to be createdNeed not name on FTP server. name name()>> 
      Label1.Text = filePath + "/" + fileName; 
      FileStream outputStream = new FileStream(filePath + "/" + fileName, FileMode.Create); 
      reqFTP = (FtpWebRequest)FtpWebRequest.Create(new 
      Uri("ftp://" + ftpServerIP + "/" + fileName)); 
      reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; 
      reqFTP.UseBinary = true; 
      reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); 
      FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); 
      Stream ftpStream = response.GetResponseStream(); 
      long cl = response.ContentLength; 
      int bufferSize = 2048; 
      int readCount; 
      byte[] buffer = new byte[bufferSize]; 

      readCount = ftpStream.Read(buffer, 0, bufferSize); 
      while (readCount > 0) 
      { 
       outputStream.Write(buffer, 0, readCount); 
       readCount = ftpStream.Read(buffer, 0, bufferSize); 
      } 

      ftpStream.Close(); 
      outputStream.Close(); 
      response.Close(); 
     } 
     catch (Exception ex) 
     { 
      Label1.Text = ex.ToString(); 
     } 
    } 




public void Upload(string filename) 
    { 
     FileInfo fileInf = new FileInfo(filename); 
     string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name; 
     FtpWebRequest reqFTP; 
     // Create FtpWebRequest object from the Uri provided 
     reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP +"/" + fileInf.Name)); 
     // Provide the WebPermission Credintials 
     reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); 
     // By default KeepAlive is true, where the control connection is not closed 
     // after a command is executed. 
     reqFTP.KeepAlive = false; 
     // Specify the command to be executed. 
     reqFTP.Method = WebRequestMethods.Ftp.UploadFile; 
     // Specify the data transfer type. 
     reqFTP.UseBinary = true; 
     // Notify the server about the size of the uploaded file 
     reqFTP.ContentLength = fileInf.Length; 
     // The buffer size is set to 2kb 
     int buffLength = 2048; 
     byte[] buff = new byte[buffLength]; 
     int contentLen; 
     // Opens a file stream (System.IO.FileStream) to read the file to be uploaded 
     FileStream fs = fileInf.OpenRead(); 
     try 
     { 
      // Stream to which the file to be upload is written 
      Stream strm = reqFTP.GetRequestStream(); 
      // Read from the file stream 2kb at a time 
      contentLen = fs.Read(buff, 0, buffLength); 
      // Till Stream content ends 
      while (contentLen != 0) 
      { 
       // Write Content from the file stream to the FTP Upload Stream 
       strm.Write(buff, 0, contentLen); 
       contentLen = fs.Read(buff, 0, buffLength); 
      } 
      // Close the file stream and the Request Stream 
      strm.Close(); 
      fs.Close(); 
     } 
     catch (Exception ex) 
     { 
      //MessageBox.Show(ex.Message, "Upload Error"); 
     } 
    } 

,這些都是函數調用

protected void btnDownload_Click(object sender, EventArgs e) 
    { 
     string FullServer = "ftp://" + ftpServerIP; 
     Download(FullServer, LbxServer.SelectedValue); 
    } 

    protected void btnUpload_Click(object sender, EventArgs e) 
    { 
     Upload(LbxLocal.SelectedValue); 
    } 

感謝大家在這個問題上的任何幫助。

+0

很難肯定不知道哪條線路的代碼會生成異常,但在分配新文件流時,可能會從該類生成異常。請驗證'filePath +「/」+ fileName'是一個有效的文件名,'filePath'目錄是否存在,並且您的進程對該目錄具有寫入權限。 – kbrimington 2010-08-30 23:19:55

+0

是的,我忘了提到那條線是拋出異常,但是當我放入一個斷點時它似乎是正確的位置,我可以通過filezilla訪問,所以我假設我不需要做任何額外的事情。 「ftp://servername/DownloadEmail.aspx」是我得到的,當我檢查服務器時,它顯示的ip(servername)是正確的。 – h34dhun73r 2010-08-30 23:36:16

+0

FileStream outputStream = new FileStream(filePath +「/」+ fileName,FileMode.Create); 這是打破代碼 filepath =「ftp:// myservername」 fileName = DownloadEmail.aspx – h34dhun73r 2010-08-30 23:37:45

回答

1

我想你會發現ftp://myservername對於新的FileStream是無效的方案。請注意,您的FileStream代表本地目標,而不是遠程文件。

如果你的目的是一個遠程文件下載到本地系統,filePath應該是指一個文件夾在本地網絡(例如,C:\temp\\somecomputer\share等)

+0

謝謝,我以前試過,並沒有意識到我得到了一個不同的錯誤。但這是正確的答案,我只是無法訪問我工作中的文件夾: – h34dhun73r 2010-08-30 23:53:34