mvc檔案上傳支援批量上傳,拖拽以及預覽,檔案內容校正等

來源:互聯網
上載者:User

標籤:接收   ons   multiple   oca   color   XML   style   ror   multipart   

使用bootstrap-fileinput

使用方式:

1.nuget:Install-Package bootstrap-fileinput

2.語言本地化{下載fileinput_locale_zh.js}或者修改Fileinput中的本地化詞彙

3.檔案大小限制:修改fileinput.js中的3195行 maxFilePreviewSize配置節點

樣本:前台

@{    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width" />    <title>Index</title>    <script src="~/Scripts/jquery-1.9.1.js"></script>    <script src="~/Scripts/bootstrap.js"></script>    <link href="~/Content/bootstrap.css" rel="stylesheet" />    <script src="~/Scripts/fileinput.js"></script>    <script src="~/Scripts/fileinput_locale_zh.js"></script>    <link href="~/Content/bootstrap-fileinput/css/fileinput.css" rel="stylesheet" />    <script type="text/javascript">        $(function () {            var control = $("#txt_file");            var uploadrul = "/Home/UploadFile";            control.fileinput({                language: ‘zh‘, //設定語言                uploadUrl: uploadrul, //上傳的地址                allowedFileExtensions: [‘xml‘,‘docx‘],//接收的檔案尾碼                showUpload: true, //顯示批量上傳按鈕                showCaption: false,//是否顯示標題                browseClass: "btn btn-primary", //按鈕樣式                     dropZoneEnabled: true,//是否顯示拖拽地區                //minImageWidth: 50, //圖片的最小寬度                //minImageHeight: 50,//圖片的最小高度                //maxImageWidth: 1000,//圖片的最大寬度                //maxImageHeight: 1000,//圖片的最大高度                //maxFileSize: 0,//單位為kb,如果為0表示不限制檔案大小                //minFileCount: 0,                maxFileCount: 100,                enctype: ‘multipart/form-data‘,                validateInitialCount: true,                previewFileIcon: "<i class=‘glyphicon glyphicon-king‘></i>",                msgFilesTooMany: "選擇上傳的檔案數量({n}) 超過允許的最大數值{m}!",            });            //匯入檔案上傳完成之後的事件            $("#txt_file").on("fileuploaded", function (event, data, previewId, index) {            });        });    </script></head><body>    <div>         <form>            <div>                <div class="modal-header">                    <h4 class="modal-title" id="myModalLabel">[請選擇xml/docx]檔案</h4>                </div>                <div class="modal-body">                    <input type="file" name="txt_file" id="txt_file" multiple class="file-loading" />                </div>            </div>        </form>    </div></body></html>

後台:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.IO;using System.Xml;using System.Xml.Schema;namespace UploadTest.Controllers{    public class HomeController : Controller    {        //        // GET: /Home/        public ActionResult Index()        {            return View();        }        public JsonResult UploadFile()        {            uploadResult result = new uploadResult();            var oFile = Request.Files["txt_file"];            result.fileName = oFile.FileName;            Stream sm = oFile.InputStream;            byte[] bt= new byte[sm.Length];            sm.Read(bt, 0, (int)sm.Length);            FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory+oFile.FileName,FileMode.Create);            fs.Write(bt, 0, bt.Length);            fs.Close();            fs.Dispose();            sm.Close();            sm.Dispose();            return Json(result, JsonRequestBehavior.AllowGet);        }        public class uploadResult        {            public string fileName { get; set; }            public string error { get; set; }        }    }}

為了在前台正確的顯示檔案的錯誤資訊,需要給前台返回一個帶有error欄位的json,其中error欄位時必須的,否則無法顯示後台回寫的錯誤訊息

 

後台可以做到更細緻的控制,並且支援多線程上傳。

mvc檔案上傳支援批量上傳,拖拽以及預覽,檔案內容校正等

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.