.net core版 文件上傳/ 支持批量上傳拖拽及預(yù)覽功能(bootstrap fileinput上傳文件)
上篇文章給大家介紹了MVC文件上傳支持批量上傳拖拽及預(yù)覽文件內(nèi)容校驗(yàn)功能
本篇內(nèi)容主要解決.net core中文件上傳的問題 開發(fā)環(huán)境:ubuntu+vscode
1.導(dǎo)入所需要的包:nuget install bootstrap-fileinput
注意:這里的導(dǎo)包需要在終端導(dǎo)入【需要在wwwroot文件夾下執(zhí)行nuget命令】如下圖
如果發(fā)現(xiàn)沒有nuget命令,則需要通過apt-get 或者yum 給系統(tǒng)安裝nuge包管理工具,這個(gè)nuget和vscode中的插件不是一回事
2前臺(tái)頁面編寫:
index.cshtml:
@{ ViewData["Title"] = "Home Page"; Layout = null; } <script src="~/jQuery.1.9.0/Content/Scripts/jquery-1.9.0.js"></script> <script src="~/bootstrap.3.3.0/content/Scripts/bootstrap.js"></script> <link rel="stylesheet" href="~/bootstrap.3.3.0/content/Content/bootstrap.css" rel="external nofollow" > <script type="text/javascript" src="~/bootstrap-fileinput.4.3.8/content/Scripts/fileinput.js"></script> <script type="text/javascript" src="~/bootstrap-fileinput.4.3.8/content/Scripts/locales/zh.js"></script> <link rel="stylesheet" href="~/bootstrap-fileinput.4.3.8/content/Content/bootstrap-fileinput/css/fileinput.css" rel="external nofollow" > <script type="text/javascript"> $(function () { var control = $("#txt_file"); var uploadrul = "/Home/UploadFile"; control.fileinput({ language: 'zh', //設(shè)置語言 uploadUrl: uploadrul, //上傳的地址 allowedFileExtensions: ['png'],//接收的文件后綴 showUpload: true, //顯示批量上傳按鈕 showCaption: false,//是否顯示標(biāo)題 browseClass: "btn btn-primary", //按鈕樣式 dropZoneEnabled: true,//是否顯示拖拽區(qū)域 //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: "選擇上傳的文件數(shù)量({n}) 超過允許的最大數(shù)值{m}!", }); //導(dǎo)入文件上傳完成之后的事件 $("#txt_file").on("fileuploaded", function (event, data, previewId, index) { }); }); </script> </table> <div> <form> <div> <div class="modal-header"> <h4 class="modal-title" id="myModalLabel">請選擇xml文件</h4> </div> <div class="modal-body"> <input type="file" name="txt_file" id="txt_file" multiple class="file-loading" /> </div> </div> </form> </div>
基本上和asp.net mvc下邊沒有區(qū)別,只有一個(gè)地方需要特別注意一下,外部的script和css文件的引用文件需要放到wwwroot文件中,而不是項(xiàng)目的根目錄下。
預(yù)覽圖:
3.主要的區(qū)別 ,后臺(tái)
代碼如下:
public JsonResult UploadFile() { uploadResult result = new uploadResult(); try { var oFile = Request.Form.Files["txt_file"]; Stream sm=oFile.OpenReadStream(); result.fileName = oFile.FileName; if(!Directory.Exists(AppContext.BaseDirectory+"/Image/")) { Directory.CreateDirectory(AppContext.BaseDirectory+"/Image/"); } string filename=AppContext.BaseDirectory+"/Image/" + DateTime.Now.ToString("yyyymmddhhMMssss")+Guid.NewGuid().ToString() + ".png"; FileStream fs=new FileStream(filename,FileMode.Create); byte[] buffer =new byte[sm.Length]; sm.Read(buffer,0,buffer.Length); fs.Write(buffer,0,buffer.Length); fs.Dispose(); } catch(Exception ex) { result.error = ex.Message; } return Json(result); } public class uploadResult { public string fileName { get; set; } public string error { get; set; } }
在netcore中無法再通過Request.Files對象來獲取從前臺(tái)傳遞的文件,這里需要使用Request.Form.Files來獲取來自客戶端提交的文件,接下來需要一個(gè)uploadResult結(jié)構(gòu)體,給前臺(tái)返回json對象 這個(gè)結(jié)構(gòu)中必須包含error字段,用來給前臺(tái)返回錯(cuò)誤數(shù)據(jù),詳情查看官方文檔-官網(wǎng)地址
附一張最終的上傳成功保存到本地的圖片:
以上所述是小編給大家介紹的.net core版 文件上傳/ 支持批量上傳拖拽及預(yù)覽功能(bootstrap fileinput上傳文件),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
ASP.NET MVC擴(kuò)展HtmlHelper方法
這篇文章介紹了ASP.NET MVC擴(kuò)展HtmlHelper的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03(asp.net c#)DropDownList綁定后顯示對應(yīng)的項(xiàng)的兩種方法
(asp.net c#)DropDownList綁定后顯示對應(yīng)的項(xiàng)的兩種方法 其實(shí)兩個(gè)方法的思路都是一樣,都是拿id去配對2011-04-04Asp.net中斷點(diǎn)續(xù)傳的原理與實(shí)現(xiàn)方法分享
在了解HTTP斷點(diǎn)續(xù)傳的原理之前,讓我們先來了解一下HTTP協(xié)議,HTTP協(xié)議是一種基于tcp的簡單協(xié)議,分為請求和回復(fù)兩種2012-08-08時(shí)間輕松學(xué)會(huì).NET Core操作ElasticSearch7的方法
這篇文章主要介紹了時(shí)間輕松學(xué)會(huì).NET Core操作ElasticSearch7,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04