新增用户资料,需要用户上传多笔附件,所以就尝试用了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. python-flask之request的属性

    flask的request模块的属性(参考) #代码示例,仅仅是为了测试request的属性值 @app.route('/login', methods = ['GET','POST']) def l ...

  2. C# 在Winform设计一个耗时较久的任务在后台执行时的状态提示窗口

    很多时候,我们需要在窗体中执行一些耗时比较久的任务.比如:循环处理某些文件,发送某些消息等... 单纯的依靠状态栏,用户体验不佳,按下功能按钮后得不到有效的提醒,小白用户绝对会电话给你说“我点了以后就 ...

  3. loj #2508. 「AHOI / HNOI2018」游戏

    #2508. 「AHOI / HNOI2018」游戏 题目描述 一次小 G 和小 H 在玩寻宝游戏,有 nnn 个房间排成一列,编号为 1,2,…,n,相邻房间之间都有 111 道门.其中一部分门上有 ...

  4. Python里生成器的问题

    任何包含yield语句的函数称为生成器.

  5. Codeforces Round #175 (Div. 2) A~D 题解

    A.Slightly Decreasing Permutations Permutation p is an ordered set of integers p1,  p2,  ...,  pn, c ...

  6. [HNOI2004]树的计数 BZOJ 1211 prufer序列

    题目描述 输入输出格式 输入格式: 输入文件第一行是一个正整数n,表示树有n个结点.第二行有n个数,第i个数表示di,即树的第i个结点的度数.其中1<=n<=150,输入数据保证满足条件的 ...

  7. Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.

    分析:还是权限问题,所以给他加上权限就可以了!! 解决:chmod +s /bin/netstat

  8. 红蓝对抗 - 蓝队手册(BTFM)(转载)

    本文已发表在嘶吼RoarTalk,未经授权,请勿转载! http://www.4hou.com/technology/10173.html 最佳阅读体验版:https://stackedit.io/v ...

  9. 吴裕雄 python 机器学习——密度聚类DBSCAN模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import cluster from sklearn.metrics ...

  10. Android HttpURLConnection的使用+Handler的原理及典型应用

    1.介绍 总结:HttpURLConnection用来发送和接收数据. 2.ANR异常报错 (1)ANR(Application not response) 应用无响应, 主线程(UI线程) (2)如 ...