コード例 #1
0
        private void startUploadFile(object obj)
        {
            UploadInfo upInfo = (UploadInfo)obj;
            FileInfo   info   = new FileInfo(mLocFile);

            upInfo.size = info.Length.ToString();
            upInfo.md5  = MD5Util.GetMD5(mLocFile);


            TcpClient tcpClient = new TcpClient();

            try
            {
                tcpClient.Connect(SERVER_IP, SERVER_PORT1);
            }
            catch (Exception e)
            {
                MessageBox.Show("连接服务器" + SERVER_IP + " 失败");
                return;
            }
            NetworkStream ns = tcpClient.GetStream();

            /* 开启接收进程 */
            new Thread(new ParameterizedThreadStart(startRecvData)).Start(obj);

            setTitle("开始上传");
            using (FileStream fsRead = new FileStream(mLocFile, FileMode.Open))
            {
                byte[] buf = new byte[1024];
                try
                {
                    int totalSize = 0;
                    while (true)
                    {
                        int r = fsRead.Read(buf, 0, buf.Length);
                        if (r < 1)
                        {
                            break;
                        }
                        totalSize += r;
                        setTitle(String.Format("已经上传{0}%", totalSize * 100 / Int32.Parse(upInfo.size)));
                        ns.Write(buf, 0, r);
                    }
                }
                catch (Exception e)
                {
                }
            }

            setTitle(null);
            ns.Close();
            tcpClient.Close();
        }
コード例 #2
0
        private void dataGridViewMain_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
            {
                return;
            }

            if (e.ColumnIndex == 3) // 上传
            {
                String locPath = mListData[e.RowIndex].locPath;
                if (String.IsNullOrEmpty(locPath))
                {
                    MessageBox.Show("请先设置本地apk路径");
                    return;
                }

                if (!File.Exists(locPath))
                {
                    MessageBox.Show("本地apk不存在,请先生成apk文件");
                    return;
                }

                String ver        = getApkVersion(locPath);
                int    compareRet = PkgUtil.compareVersion(ver, mListData[e.RowIndex].updateInfo.version);
                if (compareRet < 0)
                {
                    MessageBox.Show("当前版本小于远程服务器版本,无法上传");
                    return;
                }

                if (MessageBox.Show("远程版本:" + mListData[e.RowIndex].updateInfo.version + "\n本地版本:" + ver + "\n确认上传吗?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    UploadInfo info = new UploadInfo();
                    info.id      = mListData[e.RowIndex].id;
                    info.version = ver;
                    mLocFile     = locPath;
                    new Thread(new ParameterizedThreadStart(startUploadFile)).Start(info);
                }
            }
            else if (e.ColumnIndex == 4) // 下载
            {
                SaveFileDialog dialog = new SaveFileDialog();
                dialog.Filter   = "apk文件| *.apk|任意文件| *.*";
                dialog.FileName = mListData[e.RowIndex].name;
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    new Thread(new ParameterizedThreadStart(startDownFile)).Start(new String[] { mListData[e.RowIndex].updateInfo.appUrl, dialog.FileName, mListData[e.RowIndex].updateInfo.size }); //  (mListData[e.RowIndex].updateInfo.appUrl, dialog.FileName))
                }
            }
        }