惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
T
Tenable Blog
Project Zero
Project Zero
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
S
Schneier on Security
Scott Helme
Scott Helme
S
Securelist
Know Your Adversary
Know Your Adversary
Vercel News
Vercel News
IT之家
IT之家
V
V2EX
F
Fortinet All Blogs
Simon Willison's Weblog
Simon Willison's Weblog
K
Kaspersky official blog
博客园_首页
T
Tailwind CSS Blog
The GitHub Blog
The GitHub Blog
Spread Privacy
Spread Privacy
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
The Register - Security
The Register - Security
有赞技术团队
有赞技术团队
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
L
LINUX DO - 热门话题
Hugging Face - Blog
Hugging Face - Blog
博客园 - 三生石上(FineUI控件)
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
CXSECURITY Database RSS Feed - CXSecurity.com
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy & Cybersecurity Law Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
S
SegmentFault 最新的问题
AWS News Blog
AWS News Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost

博客园 - 一场误会

Android 2.1 和 Android 4.4 工程目录超详细对比及详解 WinForm下CheckedListBox的数据绑定 C#中添加文本框的上标及文字大小 完全卸载Oracle数据库的方法 SQL Server 安装错误服务无法启动,安装后没有SQL Server Management Studio管理器 开发人员一定要加入收藏夹的网站 也谈物联网 一位年轻女董事长的27条忠告 ASP.NET AJAX视频教程 文件上传带多种样式的进度条 工程项目四控六管一协调 用户自定义报表 甘特图、网络图控件 哈佛图书馆墙上的名训 c#读写xml文件 - 一场误会 - 博客园 多表关联的update语句 VS2005调试问题解决方案集锦 - 一场误会 - 博客园 加密、解密.NET字符串 综采工作面设备接替计划管理系统
C#文件上传下载
一场误会 · 2009-06-15 · via 博客园 - 一场误会

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