2012-05-03 11 views
0

ストリームデータをftpサーバーにイメージとして保存する方法は?どのようにC#のFTPサーバーで画像を保存するには?

FileInfo fileInf = new FileInfo("1" + ".jpg"); 
         string uri = "ftp://" + "hostip//Data//" + fileInf.Name; 
         FtpWebRequest reqFTP; 

         // Create FtpWebRequest object from the Uri provided 
         reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(
          "ftp://" + "ipaddress//Data//" + fileInf.Name)); 

         // Provide the WebPermission Credintials 
         reqFTP.Credentials = new NetworkCredential("username", 
                   "password"); 

         // 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; ??? 
         using (var img = Image.FromStream(image)) 
         { 
          img.Save(adduser.User_Id + ".jpg", ImageFormat.Jpeg); 
         } 

私に教えてください。

+1

いただきました問題からのファイルのダウンロードのためのサンプルコードがありますか? – Likurg

答えて

1

データ(イメージ)をバイト配列に取得して送信する必要があります。 FtpWebRequest.GetResponse documentationの例では基本が示されていますが、ファイルが追加されています。他のすべてはあなたがやっていることに関連しています(あなたは追加ファイルをアップロードファイルで置き換えます)。

バイト配列に画像を取得するには、あなたが書くことができます。他に

byte[] imageBuffer = File.ReadAllBytes(imageFileName); 

すべてがドキュメントの例と非常に類似していなければなりません。ここで

0

は、FTPサーバー

Uri url = new Uri("ftp://ftp.demo.com/Image1.jpg"); 
if (url.Scheme == Uri.UriSchemeFtp) 
{ 
    FtpWebRequest objRequest = (FtpWebRequest)FtpWebRequest.Create(url); 
    //Set credentials if required else comment this Credential code 
    NetworkCredential objCredential = new NetworkCredential("FTPUserName", "FTPPassword"); 
    objRequest.Credentials = objCredential; 
    objRequest.Method = WebRequestMethods.Ftp.DownloadFile; 
    FtpWebResponse objResponse = (FtpWebResponse)objRequest.GetResponse(); 
    StreamReader objReader = new StreamReader(objResponse.GetResponseStream()); 
    byte[] buffer = new byte[16 * 1024]; 
    int len = 0; 
    FileStream objFS = new FileStream(Server.MapPath("Image1.jpg"), FileMode.Create, FileAccess.Write, FileShare.Read); 
    while ((len = objReader.BaseStream.Read(buffer, 0, buffer.Length)) != 0) 
    { 
     objFS.Write(buffer, 0, len); 
    } 
    objFS.Close(); 
    objResponse.Close(); 
} 
関連する問題