zoukankan      html  css  js  c++  java
  • 用input标签 文件,多文件上传

    单个文件,多个文件区别不大,只是需要把多个文件装在一个容器里面,循环遍历即可;

    需要注意的 input 标签中name属性,一定要指定;  在这是  fileBase 

    需要确定method必须是post ; enctype必须指定为multipart/form-data

    单文件

    HTML  ----  Using

    @using (Html.BeginForm("Load", "UPLoad", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <p>
            <input type="file" name="fileBase" value="fileBase"/>
            <input type="submit" name="name" value="提交" />
        </p>
    
    }
    

    HTML  ----  form

    <form action="/UPLoad/Load" method="post" enctype="multipart/form-data">
        <p>
            <input type="file" name="fileBase" value="fileBase" />
            <input type="submit" name="name" value="提交" />
        </p>
    </form>
    

     这两种表单,看个人需要自行选用,

    控制器

    public ActionResult Load(HttpPostedFileBase fileBase)
            {
                //判断是否获取文件
                if (fileBase != null)
                {
                    var s = fileBase.FileName;
                    //存储文件夹路径
                    var sks = "/NewFold/";
                    //判断是否存在路径
                    if (!Directory.Exists(Server.MapPath(sks)))
                        //不存在 建一个
                        Directory.CreateDirectory(Server.MapPath(sks));
                    fileBase.SaveAs(Server.MapPath(sks + s));
                }
                return View();
            } 

    多文件

    HTML---using

    只是在input 标签中加  multiple 属性  就是下面这样:          form 标签中也是这样 

    @using (Html.BeginForm("Load", "UPLoad", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <p>
            <input type="file" name="fileBase" value="fileBase" multiple />
            <input type="submit" name="name" value="提交" />
        </p>
    
    }
    

    控制器

    public ActionResult Load(IEnumerable<HttpPostedFileBase> fileBase)
            {
                if (fileBase != null)
                {
                    foreach (var item in fileBase)
                    {
                        var s = item.FileName;
                        var sks = "/NewFold/";
                        if (!Directory.Exists(Server.MapPath(sks)))
                            Directory.CreateDirectory(Server.MapPath(sks));
                        item.SaveAs(Server.MapPath(sks + s));
                    }
                }
                return View();
            }

    可以试试!

  • 相关阅读:
    linux系统中如何进入退出vim编辑器,方法及区别
    [转]JAVA的动态代理机制及Spring的实现方式
    mybaties 缓存
    全面分析 Spring 的编程式事务管理及声明式事务管理
    面试(4)-spring-Spring面试题和答案
    vector的多套遍历方案
    【QT】QT下载与安装
    【QT】无需写connect代码关联信号和槽函数
    【QT】第一个QT程序(点击按钮,显示特定文本)
    【QT】error: 'SIGNAL' was not declared in this scope
  • 原文地址:https://www.cnblogs.com/Ghajini-x/p/10721938.html
Copyright © 2011-2022 走看看