bootstrap fileinput+MVC 上傳多文件,保存


新增用戶資料,需要用戶上傳多筆附件,所以就嘗試用了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中調用該控件:

 1 var url = rootUrl + "attachment/upload";
 2 $("#attach").fileinput({
 3     theme: "explorer", //主題  
 4     language: 'zh',
 5     uploadUrl: url,// 上傳請求路徑  
 6     allowedFileExtensions : ["pdf", "doc","docx"],//允許上傳的文件后綴           
 7     showUpload: false,//是否顯示上傳按鈕  
 8     showCaption: false,//是否顯示容器  
 9     dropZoneEnabled: false,//是否顯示拖拽區域   
10     removeFromPreviewOnError: true,//是否移除校驗文件失敗的文件  
11     layoutTemplates: {     
12         actionUpload: '',   //取消上傳按鈕 
13         actionZoom: ''      //取消放大鏡按鈕 
14     },
15     showPreview: true,      //顯示預覽     
16     minFileCount: 1,   //最低文件數量  
17     //maxFileCount: 3,   //最多文件數量  
18     maxFileSize: 1024*2,  //允許文件上傳大小  
19     overwriteInitial: false,
20     previewFileIcon: '<i class="fa fa-file"></i>',
21     initialPreviewAsData: true, // defaults markup    
22     preferIconicPreview: true, // 是否優先顯示圖標  false 即優先顯示圖片  
23     previewFileIconSettings: { // configure your icon file extensions  
24         'doc': '<i class="fa fa-file-word-o text-primary"></i>',
25         'docx': '<i class="fa fa-file-word-o text-primary"></i>',
26         'xls': '<i class="fa fa-file-excel-o text-success"></i>',
27         'xlsx': '<i class="fa fa-file-excel-o text-success"></i>',
28         'ppt': '<i class="fa fa-file-powerpoint-o text-danger"></i>',
29         'pdf': '<i class="fa fa-file-pdf-o text-danger"></i>',
30         'zip': '<i class="fa fa-file-archive-o text-muted"></i>',
31         'htm': '<i class="fa fa-file-code-o text-info"></i>',
32         'txt': '<i class="fa fa-file-text-o text-info"></i>',
33         'mov': '<i class="fa fa-file-movie-o text-warning"></i>',
34         'mp3': '<i class="fa fa-file-audio-o text-warning"></i>',
35         'jpg': '<i class="fa fa-file-photo-o text-danger"></i>',
36         'gif': '<i class="fa fa-file-photo-o text-muted"></i>',
37         'png': '<i class="fa fa-file-photo-o text-primary"></i>'
38     }
39 });

上傳的url就是點擊Upload按鈕調用的方法,我們沒有使用這個url,我們是在提交之后再上傳的,所以可以忽略上傳

頁面提交保存后台:

 1         [HttpPost]
 2         [ValidateInput(false)]
 3         [ValidateAntiForgeryToken]
 4         [UIExceptionResult]
 5         public ActionResult attachment_create(create model)
 6         {
 7             if (ModelState.IsValid)
 8             {
 9                
10                     string uploadPath = Server.MapPath(string.Format("~\\upload\\{0}\\", DateTime.Now.ToString("yyyyMMdd")));
11                     string savePath = string.Format("/upload/{0}/", DateTime.Now.ToString("yyyyMMdd"));
12                     if (Directory.Exists(uploadPath) == false)
13                     {
14                         Directory.CreateDirectory(uploadPath);
15                     }
16                     if (model.attch != null && model.attch.Count() > 0)
17                     {
18                         for (int i = 0; i < model.attch.Count(); i++)
19                         {
20                             var _file = model.attch[i];
21                             string _imageName = DateTime.Now.Ticks.ToString() + System.IO.Path.GetExtension(_file.FileName);
22                             var path = uploadPath + _imageName;
23                             //保存
24                             _file.SaveAs(path);
25                         }
26 
27                     }
28             return View(model);
29         }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM