C# 上传 下载 源代码

来源:互联网 发布:淘宝查假冒伪劣处罚 编辑:程序博客网 时间:2024/06/01 08:39

   //// <summary>
        /// WebClient上传文件至服务器
        /// </summary>
        /// <param name="fileNamePath">文件名,全路径格式</param>
        /// <param name="uriString">服务器文件夹路径</param>
        private void UpLoadFile(string fileNamePath, string uriString)
        {
            //改名字
            string NewFileName = txtMC.Text.Trim() + fileNamePath.Substring(fileNamePath.LastIndexOf("."));

            string fileNameExt = fileNamePath.Substring(fileNamePath.LastIndexOf(".") + 1);
            if (uriString.EndsWith("/") == false) uriString = uriString + "/";

            uriString = uriString + NewFileName;
            /**/
            /// 创建WebClient实例
            WebClient myWebClient = new WebClient();
            myWebClient.Credentials = CredentialCache.DefaultCredentials;

            // 要上传的文件
            FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
            //FileStream fs = OpenFile();
            BinaryReader r = new BinaryReader(fs);
            try
            {
                //使用UploadFile方法可以用下面的格式
                //myWebClient.UploadFile(uriString,"PUT",fileNamePath);
                byte[] postArray = r.ReadBytes((int)fs.Length);
                Stream postStream = myWebClient.OpenWrite(uriString, "PUT");
                if (postStream.CanWrite)
                {
                    postStream.Write(postArray, 0, postArray.Length);
                    MessageBox.Show("上传成功!");
                }
                else
                {
                    MessageBox.Show("文件目前不可写!");
                }
                postStream.Close();
            }
            catch
            {
                MessageBox.Show("文件上传失败,请稍候重试~");
                return;
            }
        }

 

   /// <summary>
        /// 下载服务器文件至客户端

        /// </summary>
        /// <param name="URL">被下载的文件地址,绝对路径</param>
        /// <param name="Dir">另存放的目录</param>
        public void Download(string URL, string Dir)
        {
            WebClient client = new WebClient();
            string fileName = URL.Substring(URL.LastIndexOf("\\") + 1); //被下载的文件名

            string Path = Dir + fileName;   //另存为的绝对路径+文件名

            try
            {
                WebRequest myre = WebRequest.Create(URL);
            }
            catch
            {
                //MessageBox.Show(exp.Message,"Error");
            }

            try
            {
                client.DownloadFile(URL, Path);

            }
            catch
            {
                //MessageBox.Show(exp.Message,"Error");
            }
        }

 

 

  //上传事件
        private void btnsc_Click(object sender, EventArgs e)
        {
            if (txtMC.Text.Trim() == "")
            {
                MessageBox.Show("请输入文件名称!");
                return;
            }
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = "c:\\";
            openFileDialog.Filter = "文件类型|*.*";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtlj.Text = openFileDialog.FileName;
                if (txtlj.Text.Trim() != "")
                {
                    string a = txtlj.Text.Trim();
                    string c = a.Substring(a.LastIndexOf(".") + 1, 3);
                    if (c != "pdf")
                    {
                        MessageBox.Show("仅支持pdf文件!");
                        txtlj.Text = "";
                        return;
                    }
                    string aa = Application.StartupPath;
                    string bb = aa.Substring(0, aa.LastIndexOf("\\"));
                    string cc = bb.Substring(0, bb.LastIndexOf("\\"));
                    UpLoadFile(txtlj.Text.Trim(), cc + "\\ZZJH_PDF");
                }
            }
        }

 

    //下载事件
        private void btnLode_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                txtlj.Text = folderBrowserDialog.SelectedPath;
                string aa = Application.StartupPath;
                string bb = aa.Substring(0, aa.LastIndexOf("\\"));
                string cc = bb.Substring(0, bb.LastIndexOf("\\"));
                string sc = txtlj.Text.Trim() + "\\";
                string pa = txtMC.Text.Trim() + ".pdf";
                Download(cc + "\\ZZJH_PDF\\" + pa + "",sc);
                MessageBox.Show("下载成功!");
            }        
          
        }

原创粉丝点击