• WPF上传文件到服务器


    利用WebClient 上传文件到服务器

    创建一个空网站,创建一个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         }
  • 相关阅读:
    SXOI2016 部分解题报告
    两道FFT的应用题
    [CQOI2012]交换棋子【网络流】【费用流】
    JAVA-SDK-Excel4j使用遇见的问题
    解决Zookeeper出现Error: Could not find or load main class org.apache.zookeeper.server.quorum.QuorumPeerMain问题
    maven项目打包时jar中不包含依赖
    CentOS_7中的zookeeper安装
    SpringBoot集成Redis出现WRONGTYPE Operation against a key holding the wrong kind of value错误
    主机访问虚拟机中Redis
    使用SpringS声明式的开启事务
  • 原文地址:https://www.cnblogs.com/liuph/p/5996247.html
Copyright © 2020-2023  润新知