zoukankan      html  css  js  c++  java
  • C#文件上传下载

    1、文件上传 

    /// <summary>
            /// WebClient上传文件至服务器
            /// </summary>
            /// <param name="fileNamePath">文件名,全路径格式</param>
            /// <param name="uriString">服务器文件夹路径</param>
            private void UpLoadFile(string fileNamePath, string uriString)
            {
                string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("\\") + 1);
                NewFileName = DateTime.Now.ToString("yyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + fileNamePath.Substring(fileNamePath.LastIndexOf("."));
                string fileNameExt = fileName.Substring(fileName.LastIndexOf(".") + 1);
                if (uriString.EndsWith("/") == false) uriString = uriString + "/";
                uriString = uriString + NewFileName;
                UpFileName = 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);
                    }
                    else
                    {
                        MessageBox.Show("文件目前不可写!");
                    }
                    postStream.Close();
                }
                catch
                {
                    MessageBox.Show("文件上传失败,请稍候重试~");
                }
            }

    2、文件下载

      string filePath = System.AppDomain.CurrentDomain.BaseDirectory.ToString()+"ProgramOut.xml";//路径 
                    WebClient client = new WebClient();
                    string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1); //被下载的文件名
                    SaveFileDialog saveFileDialog = new SaveFileDialog();
                    saveFileDialog.Filter = "XML文件|*.xml";
                    saveFileDialog.FilterIndex = 2;
                    saveFileDialog.RestoreDirectory = true;
                    if (saveFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        string fName = saveFileDialog.FileName;
                        string Path = fName; //另存为的绝对路径+文件名
                        try
                        {
                            WebRequest myre = WebRequest.Create(Path);
                        }
                        catch
                        {
                            MessageBox.Show("文件下载失败", "Error");
                        }
                        try
                        {
                            client.DownloadFile(filePath, Path);
                            MessageBox.Show("文件导出成功!!", "系统提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        catch
                        {
                            MessageBox.Show("文件下载失败", "Error");
                        }

  • 相关阅读:
    基本技能训练之线程
    关于UEditor的使用配置(图片上传配置)
    PAT 乙级练习题1002. 写出这个数 (20)
    codeforces 682C Alyona and the Tree DFS
    codeforces 681D Gifts by the List dfs+构造
    codeforces 678E Another Sith Tournament 概率dp
    codeforces 680E Bear and Square Grid 巧妙暴力
    codeforces 678D Iterated Linear Function 矩阵快速幂
    codeforces 679A Bear and Prime 100 交互
    XTUOJ 1248 TC or CF 搜索
  • 原文地址:https://www.cnblogs.com/huangwen/p/1503421.html
Copyright © 2011-2022 走看看