2015-04-23 196 views
1

我已經得到了下面的代碼,我需要上傳多個沒有分配給它們的擴展名/類型的文件。下面的下面的代碼工作,但不能在這些文件,因爲它是試圖使打了個比方一個文件:ftp.server/fileC#上傳文件沒有擴展名

這裏是我的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.Net; 
using System.Threading.Tasks; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      String sourcefilepath = @"path"; // e.g. 「d:/test.docx」 
      String ftpurl = @"ftp"; // e.g. ftp://serverip/foldername/foldername 
      String ftpusername = @"user"; // e.g. username 
      String ftppassword = @"pass"; // e.g. password 

      string[] filePaths = Directory.GetFiles(sourcefilepath); 

      // Get the object used to communicate with the server. 
      foreach (string filename in filePaths) 
      { 
       FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpurl + Path.GetFileName(filename)); 
       request.Method = WebRequestMethods.Ftp.UploadFile; 

       // This example assumes the FTP site uses anonymous logon. 
       request.Credentials = new NetworkCredential(ftpusername, ftppassword); 

       // Copy the contents of the file to the request stream. 
       StreamReader sourceStream = new StreamReader(sourcefilepath); 
       byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); 
       sourceStream.Close(); 
       request.ContentLength = fileContents.Length; 

       Stream requestStream = request.GetRequestStream(); 
       requestStream.Write(fileContents, 0, fileContents.Length); 
       requestStream.Close(); 

       FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 

       Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription); 

       response.Close(); 
      } 
     } 
    } 
} 

的文件是從iPhone備份,iPhone備份別不提供文件擴展名,所以我需要解決。

+0

爲什麼不只是給你的文件的虛擬擴展,比如'.dummy' –

+1

另外,你或許不應該發佈您的FTP地址,用戶名和密碼,SO –

+1

我也會改變他們,以及萬一 –

回答

0

我找到的解決方案是複製文件並添加一個虛擬擴展名。