zoukankan      html  css  js  c++  java
  • C# 前端多次上传文件

     

    [置顶] asp.net/c# 用<input type="file" />实现文件上传,multipart/form-data

    标签: input typefile文件上传asp.netc#multipartform-data
     分类:

    <input type="file" />我们常用的上传文件的工具(控件),它和 <asp:FileUpload ID="FileUpload1" runat="server" />不一样,在后台不能直接获取到,不能像

    this.FileUpload1.PostedFile……那样去获取

    而有时我们必须使用<input type="file" />,如动态给页面添加好多个<input type="file" />,我们后台要怎么获取呢

    [html] view plain copy
     
     print?
    1. <html xmlns="http://www.w3.org/1999/xhtml">  
    2. <head runat="server">  
    3.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
    4.     <title></title>  
    5. </head>  
    6. <body>  
    7.     <form runat="server" id="form1" method="post" >  
    8.         <input name="f" type="file" />  
    9.         <input name="s" type="submit" />  
    10.     </form>  
    11. </body>  
    12. </html>  


    后台代码:

    [csharp] view plain copy
     
     print?
    1. //客户端上传的文件  
    2.  System.Web.HttpFileCollection _file = System.Web.HttpContext.Current.Request.Files;  
    3.  if (_file.Count > 0)  
    4.  {  
    5.      //文件大小  
    6.      long size = _file[0].ContentLength;  
    7.      //文件类型  
    8.      string type = _file[0].ContentType;  
    9.      //文件名  
    10.      string name = _file[0].FileName;  
    11.      //文件格式  
    12.      string _tp = System.IO.Path.GetExtension(name);  
    13.   
    14.      if (_tp.ToLower() == ".jpg" || _tp.ToLower() == ".jpeg" || _tp.ToLower() == ".gif" || _tp.ToLower() == ".png" || _tp.ToLower() == ".swf")  
    15.      {  
    16.          //获取文件流  
    17.          System.IO.Stream stream = _file[0].InputStream;  
    18.          //保存文件  
    19.          string saveName = DateTime.Now.ToString("yyyyMMddHHmmss") + _tp;  
    20.          string path = DataFactory.WFile.FileUploadPath + "/upload/area/" + saveName;  
    21.          _file[0].SaveAs(path);  
    22.      }  
    23.  }  


    写成这样,我们发现每次获得的_file.Count 都是0

    我们需要为form加上enctype="multipart/form-data"的属性

    表单中enctype="multipart/form-data"的意思,是设置表单的MIME编码。默认情况,这个编码格式是application/x-www-form-urlencoded,不能用于文件上传;只有使用了

    multipart/form-data,才能完整的传递文件数据。

    修改代码如下:

    [html] view plain copy
     
     print?
    1. <html xmlns="http://www.w3.org/1999/xhtml">  
    2. <head runat="server">  
    3.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
    4.     <title></title>  
    5. </head>  
    6. <body>  
    7.     <form runat="server" id="form1" method="post" enctype="multipart/form-data">  
    8.         <input name="f" type="file" />  
    9.         <input name="s" type="submit" />  
    10.     </form>  
    11. </body>  
    12. </html>  


     

    后台获取到了Request.Files

    我们为form 加上runat="server" action可以指向其他页面

    总结:

    1.form 必须有runat="server"标记,

    2.form  必须有enctype="multipart/form-data"标记,

    3.<input type="file" />的runat="server"标记可选

  • 相关阅读:
    NetCore使用Log4Net记录日志
    WCF数据协议中XmlArrayItem标签不起作用的问题
    WTM Blazor,Blazor开发利器
    WTM5.0发布,全面支持.net5
    log4netdemo
    mes 入库单号 锁表方案
    线程基础篇-线程同步
    线程基础篇-线程和进程
    EF基础篇-Code First
    EF基础篇-DB First
  • 原文地址:https://www.cnblogs.com/xiaocandou/p/5158710.html
Copyright © 2011-2022 走看看