新增用户资料,需要用户上传多笔附件,所以就尝试用了fileinput控件,显示效果如图:

首先,先在model中定义数据模型:

public partial class create
{
[Required]
[Display(Name = "附件")]
public HttpPostedFileBase[] attach { get; set; }

视图中定义控件:

<div class="form-group">
@Html.LabelFor(m => m.attach, new { @class = "col-sm-3 control-label" })
<div class="col-sm-7">
@Html.TextBoxFor(model => model.attach, new { type = "file", multiple = "multiple", accept = "application/msword,application/pdf,application/vnd.openxmlformats-officedocument.wordprocessingml.document" })
@Html.ValidationMessageFor(m => m.attach, "", new { @class = "text-danger" })
</div>
</div>

该控件的类型是multiple,可以选择多个文件;accept属性是可选择的文件类型,这里我们要去只能选择doc docx pdf ,需支持其他类型的可自行百度;ValidationMessageFor是必填

js中调用该控件:

 var url = rootUrl + "attachment/upload";
$("#attach").fileinput({
theme: "explorer", //主题
language: 'zh',
uploadUrl: url,// 上传请求路径
allowedFileExtensions : ["pdf", "doc","docx"],//允许上传的文件后缀
showUpload: false,//是否显示上传按钮
showCaption: false,//是否显示容器
dropZoneEnabled: false,//是否显示拖拽区域
removeFromPreviewOnError: true,//是否移除校验文件失败的文件
layoutTemplates: {
actionUpload: '', //取消上传按钮
actionZoom: '' //取消放大镜按钮
},
showPreview: true, //显示预览
minFileCount: 1, //最低文件数量
//maxFileCount: 3, //最多文件数量
maxFileSize: 1024*2, //允许文件上传大小
overwriteInitial: false,
previewFileIcon: '<i class="fa fa-file"></i>',
initialPreviewAsData: true, // defaults markup
preferIconicPreview: true, // 是否优先显示图标 false 即优先显示图片
previewFileIconSettings: { // configure your icon file extensions
'doc': '<i class="fa fa-file-word-o text-primary"></i>',
'docx': '<i class="fa fa-file-word-o text-primary"></i>',
'xls': '<i class="fa fa-file-excel-o text-success"></i>',
'xlsx': '<i class="fa fa-file-excel-o text-success"></i>',
'ppt': '<i class="fa fa-file-powerpoint-o text-danger"></i>',
'pdf': '<i class="fa fa-file-pdf-o text-danger"></i>',
'zip': '<i class="fa fa-file-archive-o text-muted"></i>',
'htm': '<i class="fa fa-file-code-o text-info"></i>',
'txt': '<i class="fa fa-file-text-o text-info"></i>',
'mov': '<i class="fa fa-file-movie-o text-warning"></i>',
'mp3': '<i class="fa fa-file-audio-o text-warning"></i>',
'jpg': '<i class="fa fa-file-photo-o text-danger"></i>',
'gif': '<i class="fa fa-file-photo-o text-muted"></i>',
'png': '<i class="fa fa-file-photo-o text-primary"></i>'
}
});

上传的url就是点击Upload按钮调用的方法,我们没有使用这个url,我们是在提交之后再上传的,所以可以忽略上传

页面提交保存后台:

         [HttpPost]
[ValidateInput(false)]
[ValidateAntiForgeryToken]
[UIExceptionResult]
public ActionResult attachment_create(create model)
{
if (ModelState.IsValid)
{ string uploadPath = Server.MapPath(string.Format("~\\upload\\{0}\\", DateTime.Now.ToString("yyyyMMdd")));
string savePath = string.Format("/upload/{0}/", DateTime.Now.ToString("yyyyMMdd"));
if (Directory.Exists(uploadPath) == false)
{
Directory.CreateDirectory(uploadPath);
}
if (model.attch != null && model.attch.Count() > )
{
for (int i = ; i < model.attch.Count(); i++)
{
var _file = model.attch[i];
string _imageName = DateTime.Now.Ticks.ToString() + System.IO.Path.GetExtension(_file.FileName);
var path = uploadPath + _imageName;
//保存
_file.SaveAs(path);
} }
return View(model);
}

bootstrap fileinput+MVC 上传多文件,保存的更多相关文章

  1. .Net mvc 上传多文件

    .net mvc 上传多文件有很多种方式,我的方法只是其中一种, 仅供参考,我主要是注重参数传递的过程,后面文件保存的地方省略.. 调试环境 vs2017 控制器代码: [HttpPost] publ ...

  2. asp.net mvc 上传下载文件的几种方式

    view: <!DOCTYPE html> <html> <head> <meta name="viewport" content=&qu ...

  3. spring mvc上传下载文件

    前端jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...

  4. bootstrap fileinput添加上传成功回调事件

    国外牛人做的bootstrap fileinput挺酷的,但是可惜没有提供自定义上传成功回调事件的接口,因此感到非常头疼,但是很幸运的是,我在网上搜索到一个提问帖子,它问到使用Jquery的on函数绑 ...

  5. ASP.NET MVC 上传大文件时404

    前一段时间会员的上传组件改用FLASH的swfupload来上传,既能很友好的显示上传进度,又能完全满足大文件的上传. 后来服务器升级到windows 2008,改为IIS7后,上传文件一旦超过30M ...

  6. asp.net core mvc上传大文件解决方案

    默认上传文件大小不超过30M 第一个问题: IIS 10.0 详细错误 - 404.13 - Not Found 请求筛选模块被配置为拒绝超过请求内容长度的请求. 服务器上的请求筛选被配置为拒绝该请求 ...

  7. MVC上传(单文件)

    后台代码: public ActionResult upload() { //获取文件对象 var file = Request.Files[0]; string suffix/*文件格式*/ = S ...

  8. mvc 上传大文件

    <configuration> <system.web> <httpRuntime maxRequestLength="204800" useFull ...

  9. jquery.form.js mvc 上传文件 layer 选择框与等待效果

    HTML <form role="form" id="form1"> <div class="form-group"> ...

随机推荐

  1. iOS APP打包上传到APPstore的最新步骤

    一.前言: 作为一名iOS开发者,把辛辛苦苦开发出来的App上传到App Store是件必要的事.但是很多人还是不知道该怎么上传到App Store上 下面就来详细讲解一下具体流程步骤. 二.准备: ...

  2. windows10 docker 使用以及虚拟机下fastDFS 的使用教程

    我们 先来了解下 什么是docker?   Docker的思想来自于集装箱,集装箱解决了什么问题?在一艘大船上,可以把货物规整的摆放起来.并且各种各样的货物被集装箱标准化了,集装箱和集装箱之间不会互相 ...

  3. 3.mouseenter和mouseover事件的区别

    <html> <head> <meta charset="UTF-8"> <script src="jquery-3.3.1.j ...

  4. 最小生成树 & 洛谷P3366【模板】最小生成树 & 洛谷P2820 局域网

    嗯... 理解生成树的概念: 在一幅图中将所有n个点连接起来的n-1条边所形成的树. 最小生成树: 边权之和最小的生成树. 最小瓶颈生成树: 对于带权图,最大权值最小的生成树. 如何操作? 1.Pri ...

  5. 20多个常用的免费WebService接口

    20多个常用的免费WebService接口 天气预报Web服务,数据来源于中国气象局 Endpoint  Disco  WSDL IP地址来源搜索 WEB 服务(是目前最完整的IP地址数据) Endp ...

  6. windows 下 redis 安装

    百度下载 windows 版本 的 redis... 解压 安装为windows服务 安装命令: redis-server.exe --service-install redis.windows.co ...

  7. Windows10 下安装配置IIS + MySQL5.7.19 + nginx1.12.1 + php7.1.7

    环境: VMWare Workstation Player12 Windows10 Pro x64 一.安装系统 vmware 会采用 fast install 方式很快装完,无需配置什么. 二.配置 ...

  8. 克隆kvm虚拟机报错ImportError: No module named 'requests.packages.urllib3'

    2018-06-21 更新系统造成kvm克隆命令报错 virt-clone -o centos--update-clone -n centos--maven-test -f /var/lib/vmdk ...

  9. redis cluster 集群部署

    准备工作 1. 安装docker curl -s https://get.docker.com/ | sh 注:一键安装的事最新版docker.已安装docker可跳过此步骤 2. 获取基础镜像 do ...

  10. 获取各站点的ID

    using (var serverManager = new ServerManager()) { foreach(var site in serverManager.Sites) { Console ...