您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān).net core版上傳文件/ 批量上傳拖拽及預(yù)覽功能的實(shí)現(xiàn)方法的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過來看看吧。
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)頁(yè)面編寫:
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è)置語(yǔ)言 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> <p> <form> <p> <p class="modal-header"> <h5 class="modal-title" id="myModalLabel">請(qǐng)選擇xml文件</h5> </p> <p class="modal-body"> <input type="file" name="txt_file" id="txt_file" multiple class="file-loading" /> </p> </p> </form> </p>
基本上和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中無(wú)法再通過Request.Files對(duì)象來獲取從前臺(tái)傳遞的文件,這里需要使用Request.Form.Files來獲取來自客戶端提交的文件,接下來需要一個(gè)uploadResult結(jié)構(gòu)體,給前臺(tái)返回json對(duì)象 這個(gè)結(jié)構(gòu)中必須包含error字段,用來給前臺(tái)返回錯(cuò)誤數(shù)據(jù),詳情查看官方文檔-官網(wǎng)地址
附一張最終的上傳成功保存到本地的圖片:
感謝各位的閱讀!關(guān)于.net core版上傳文件/ 批量上傳拖拽及預(yù)覽功能的實(shí)現(xiàn)方法就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。