• MVC框架实现文件的上传(支持多文件上传)


    MVC框架实现文件的上传(支持多文件上传)

    controllers/View:

    (Controllers方法可以公用,只需增加View里面的<input type="file" name="FileUpload1" value="dataURL"/>)

    <% 
     using (Html.BeginForm("", "home", FormMethod.Post, new {enctype="multipart/form-data"})) 
      {%> 
       <input type="file" name="FileUpload1" /><br /> 
       <input type="file" name="FileUpload2" /><br /> 
       <input type="file" name="FileUpload3" /><br /> 
       <input type="file" name="FileUpload4" /><br /> 
       <input type="file" name="FileUpload5" /><br /> 
       <input type="submit" name="Submit" id="Submit" value="Upload" /> 
    <% }%>
    LinkBotton事件
    <a href="<%=Url.Action("DeleteUser")%>?id=<%=Model.UserId%>" >删除</a

    ----------------------------------------------------------------------------------------------

    // <summary>
    /// 显示文件列表
    /// </summary>
    /// <returns></returns>
    public ViewResult List()
    {
      UpLoadFile f = new UpLoadFile();
      List<UpLoadFile> list = f.List();
      ViewBag.list = list;
      return View("Upload");
    }
       
    /// <summary>
    /// 接收文件,并保存到数据库中的方法
    /// </summary>
    /// <returns></returns>
    public ViewResult Upload()
    {
      for (int i = 0; i < Request.Files.Count; i++)
      {
        HttpPostedFileBase file = Request.Files[i];
        //存入文件
        if (file.ContentLength > 0)
        {
          file.SaveAs(Server.MapPath("~/") + System.IO.Path.GetFileName(file.FileName));
        }

        //存入数据库
        if (file.ContentLength > 0)
        {
          //得到文件数组
          byte[] fileData = new Byte[file.ContentLength];
          file.InputStream.Position = 0; //此句很关键
          file.InputStream.Read(fileData, 0, file.ContentLength);
          //得到文件大小
          int fileLength = file.ContentLength;
          //得到文件名字
          string fileName = System.IO.Path.GetFileName(file.FileName);

          //得到文件类型
          string fileType = file.ContentType;

          //自定义文件对象
          UpLoadFile f = new UpLoadFile();
          f.PersonImage = fileData;
          f.PersonName = fileName;
          f.PersonImageType = fileType;
          f.PersonImageSize = fileLength;
          f.PersonImageFileName = fileName;
          f.SaveToDataBase();         
        }
      }
      return View(List());
    }
        
    /// <summary>
    /// 显示图片
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public FileContentResult Display(int id)
    {
      UpLoadFile f = new UpLoadFile();
      f.LoadById(id);
      return new FileContentResult(f.PersonImage, f.PersonImageType);
    }
    4,视图文件,显示列表和上传表单

    Upload.cshtml 代码
    <!DOCTYPE html>
    <html>
    <head>
      <title>多文件上传</title>
    </head>
    <body>
      <h2>多文件上传</h2>
      <table border="1">
      <thead>
      <th>文件名</th>
      <th>文件类型</th>
      <th>浏览</th>
      </thead>
      @* 对于列表显示页面,进行特殊的处理,如果没有记录,则不显示列表。 *@
      @if (ViewBag.list != null)
      {    
        <tbody>
          @for (int i = 0; i < ViewBag.list.Count; i++)
          {
            <tr>
           <td>@ViewBag.list[i].PersonName</td>
           <td>@ViewBag.list[i].PersonImageType</td>
           <td><img alt="@ViewBag.list[i].PersonName" src='@Url.Action("Display", "Home", new { id = ViewBag.list[i].PersonId })' /></td>
            </tr>
          }
        </tbody>
      }  
      </table>
      <form action="/Home/Upload" enctype="multipart/form-data" method="post">
      <input type="file" name="f1" /><br />
      <input type="file" name="f2" /><br />
      <input type="submit" value="多文件上传" />
      </form>
    </body>
    </html>

  • 相关阅读:
    一份面试题+整理的答案
    java获取本机IPV4地址,非127.0.0.1
    List去重
    禁止iframe页面时产生历史记录
    js打开新窗口并且不被拦截
    ifream 跨域实现高度自适应
    css版tooltip
    jQuery遮罩插件 jQuery.blockUI.js
    SpringMVC 传递相同名称的参数
    SpringMVC接收List型参数
  • 原文地址:https://www.cnblogs.com/mbtq/p/2765190.html
Copyright © 2020-2023  润新知