C#上传与接收文件

客户端

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;
            }

你可能感兴趣的:(C#)