码迷,mamicode.com
首页 > Windows程序 > 详细

WPF上传文件到服务器

时间:2016-10-25 14:27:08      阅读:1243      评论:0      收藏:0      [点我收藏+]

标签:wpf   substring   web   repo   query   post   成功   name   var   

创建一个空网站,创建一个UploadFile.aspx项,

服务器报500错误时,请检查文件保存路径是否存在

 1     protected void Page_Load(object sender, EventArgs e)
 2        {
 3            foreach (string f in Request.Files.AllKeys)
 4            {    //在客户端传入新的文件  
 5                HttpPostedFile file = Request.Files[f];
 6                //在客户端传入一个新的文件名               
 7                string directory = Request.QueryString["d"];
 8                string filename = Request.QueryString["n"];
 9                //file.SaveAs(Server.MapPath("../ReportFile/" + filename + file.FileName.Substring(file.FileName.IndexOf("."))));
10                string path = string.Format(@"G:\ReportFile\{0}\", directory);
11                if (!Directory.Exists(path)) { Directory.CreateDirectory(path); }
12                file.SaveAs(path + filename);
13            }
14        }

WPF中

 1 using form = System.Windows.Forms;
 2         private const string directory = "0123";
 3         public void Upload(string file)
 4         {
 5             FileInfo info = new FileInfo(file);
 6             string url = string.Format("http://192.168.31.118:54040/UploadFile.aspx?d={0}&n={1}", directory, info.Name);
 7             WebClient client = new WebClient();
 8             client.Credentials = CredentialCache.DefaultCredentials; //获取或设置发送到主机并用于请求进行身份验证的网络凭据  
 9             client.UploadFileAsync(new Uri(url), file);
10             client.UploadFileCompleted += new UploadFileCompletedEventHandler(result_UploadFileCompleted);
11         }
12         private void result_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
13         {
14             if (e.Error != null)
15             {
16                 MessageBox.Show("上传失败:" + e.Error.Message);
17             }
18             else
19             {
20                 MessageBox.Show("上传成功!");
21             }
22         }
23 
24         private void UploadFile_Click(object sender, RoutedEventArgs e)
25         {
26             form.OpenFileDialog _dialog = new form.OpenFileDialog();
27             _dialog.Multiselect = true;
28             if (_dialog.ShowDialog() == form.DialogResult.OK)
29             {
30                 string[] _files = _dialog.FileNames;
31                 if (_files != null && _files.Length > 0)
32                 {
33                     foreach (var item in _files)
34                     {
35                         Upload(item);
36                     }
37                 }
38             }
39         }

 

WPF上传文件到服务器

标签:wpf   substring   web   repo   query   post   成功   name   var   

原文地址:http://www.cnblogs.com/liuph/p/5996247.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!