新增用户资料,需要用户上传多笔附件,所以就尝试用了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. 【BZOJ3622】已经没什么好害怕的了 容斥原理+dp

    Description Input Output Sample Input 4 2 5 35 15 45 40 20 10 30 Sample Output 4 HINT 输入的2*n个数字保证全不相 ...

  2. selenium+PhantomJS简单爬虫

    #!/usr/bin/env python # -*- coding: utf-8 -*- ''' Created on 2017年10月19日 @author: zzy ''' import tim ...

  3. [USACO06DEC]牛的野餐Cow Picnic DFS

    题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N ...

  4. Linux 使用echo向文件末尾追加命令

    //echo后边用单引号包围要添加的内容 echo 'add content'>>/home/data/test.sh 注意>>表示在原来的文件末尾上进行追加,如果使用的是&g ...

  5. pycharm运行Pytest,有没有将Pytest写入Python代码中的区别

    初学pytest. 将pytest写进Python代码中 不同运行方式都可正常运行     =======================**********************========= ...

  6. abp架构添加实体并使用迁移功能生成表

    参考: https://aspnetboilerplate.com/Pages/Articles/Introduction-With-AspNet-Core-And-Entity-Framework- ...

  7. 【总结】kali(amd64)中安装nessus

    下载nessus: http://www.tenable.com/products/nessus/select-your-operating-system 注册nessus家庭版 http://www ...

  8. android 无线调试 [无需数据线][无需root]

    无线调试首要条件在同一网段,打开开发者模式 1,打开 5555 端口 使用数据线链接手机,在命令窗口执行:adb tcpip 5555 2,adb 链接手机调试 这时无需数据线了,在命令窗口执行:ad ...

  9. PIE SDK辐射定标

    1. 算法功能简介 辐射定标是使用大气纠正技术将影像数据的灰度值转化为表观辐亮度.表观反射率等物理量的过程. PIE支持算法功能的执行,下面对辐射定标算法功能进行介绍. 2. 算法功能实现说明 2.1 ...

  10. 升级TeeChart pro

    teechart 安装流程如下: 1.  将生成的 LIB中的 选中文件copy到C:\Users\Public\Documents\RAD Studio\8.0\Dcp 图1 1.  fastrep ...