C#ファイルのアップロードと受信

4345 ワード

クライアント
private void btnSelectUpdateFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openfile = new OpenFileDialog();   //    
            openfile.Filter = "";

        if (openfile.ShowDialog() == DialogResult.OK)  //       ,     
        {
            if (openfile.FileName == "" || openfile.FileName == null)    //     
            {
                return;
            }

            if (!ChargeSetupfile(openfile.FileName))    //          
            {
                MessageBox.Show("           ,     !");
                return;
            }

            txtUpdateFilePath.Text = openfile.FileName;   //       filename
        }
    }

string path = txtUpdateFilePath.Text;
if (File.Exists(path))   //         
                {
                    FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
                    StreamReader sr = new StreamReader(fs);
                    FileInfo file = new FileInfo(path);
                    int len = (int)file.Length;
                    byte[] buffer = new byte[len];
                    sr.BaseStream.Read(buffer, 0, len);
                    }
  ret = RemoteUpdateServer.Instance.UploadFileData(fileName, buffer);  //              

//
private void button14_Click(object sender, EventArgs e)
        {
            if (updataConfigDic == "")
            {
                return;
            }

            int ret = -1;
            DirectoryInfo dir = new DirectoryInfo(updataConfigDic);  // updataConfigDic     ,         
            FileInfo[] files = dir.GetFiles();   //           
            FileStream fs = null;   //   
            StreamReader sr = null;   //   
            foreach (var file in files)
            {
                string fileFullName = file.FullName;
                string fileName = file.Name;
                if (fileName != "AlarmModes.xml" && fileName != "AppSettingObject.xml")
                {
                    continue;    //      ,   ,         
                }

            //         
            fs = new FileStream(fileFullName, FileMode.OpenOrCreate);
            sr = new StreamReader(fs);
            byte[] buffer = new byte[file.Length];
            sr.BaseStream.Read(buffer, 0, (int)file.Length);

            ret = RemoteUpdateServer.Instance.UploadFileData(fileName, buffer);  //              
        }

        fs.Close();   //     
        sr.Close();   //     

        if (ret == ErrorCode.Success)   //    

 /// 
    ///       
    /// 
    public int UploadFileData(string FileName, byte[] FileData)
    {
        return CommonClient.Instance.UploadFileData(FileName, FileData);
    }

/// 
        ///       
        /// 
        public int UploadFileData(string FileName, byte[] FileData)
        {
            try
            {
                return m_RpcProxy.UploadFileData(_sessionID, FileName, FileData);   //  rpc  
            }

サービス側はrpcを受信する
public int UploadFileData(string FileName, byte[] FileData)
        {
            try
            {
                string tmpFileName = Path.GetFileName(FileName);   //       ,           ,           
                                                                   //     path              。    path                ,       System.String.Empty。    path
                                                                   //       null,       null。
                string base64Str = "";
            if (null != FileData && FileData.Length > 0)   //     
            {
                SystemUtils.DeleteFilePath(AppDomain.CurrentDomain.BaseDirectory + tmpFileName);  //     +  ,        

                base64Str = Convert.ToBase64String(FileData);   //   8                Base64               。

                int iRet = SystemUtils.WriteData2File(AppDomain.CurrentDomain.BaseDirectory + tmpFileName, base64Str);   //        
                if (ErrorCode.Success != iRet)
                {
                    return ErrorCode.Failed;   //Rpc        
                }

                return ErrorCode.Success;   //rpc        
            }
            else
            {
                return ErrorCode.InvalidArg;
            }