.Net?Core使用layui多文件上傳
本文實(shí)例為大家分享了.Net Core使用layui多文件上傳功能的具體代碼,供大家參考,具體內(nèi)容如下
這段時(shí)間剛剛接觸了.NET Core,工作要求,從0開(kāi)始,給用戶(hù)開(kāi)發(fā)了一個(gè)小型的內(nèi)部系統(tǒng)。用戶(hù)提出需求,要求能實(shí)現(xiàn)多文件上傳,上傳不同位置的文件,可以刪除。
找來(lái)找去還是layui的文件上傳符合審美,不多廢話上代碼
1.前端頁(yè)面
<div class="layui-upload"> ? ? ?<button type="button" class="layui-btn layui-btn-normal" id="testList">Search</button> ? ? ?<div class="layui-upload-list"> ? ? ? ? ? ?<table class="layui-table"> ? ? ? ? ? ? <thead> ? ? ? ? ? ? ? ?<tr> ? ? ? ? ? ? ? ?<th>File Name</th> ? ? ? ? ? ? ? ?<th>Size</th> ? ? ? ? ? ? ? ?<th>Status</th> ? ? ? ? ? ? ? ?<th>Action</th> ? ? ? ? ? ? ? ?</tr> ? ? ? ? ? ? </thead> ? ? ? ? ? ? <tbody id="demoList"></tbody> ? ? ? ? ?</table> ? ? ? </div> ? <button type="button" class="layui-btn" id="testListAction" onclick="noFile()">Upload</button> </div>
script部分
<script>
? ?layui.use('upload', function () {
? ? ? ? var upload = layui.upload;
? ? ? ? var demoListView = $('#demoList')
? ? ? ? ? ? , uploadListIns = upload.render({
? ? ? ? ? ? ? ? elem: '#testList'
? ? ? ? ? ? ? ? , url: '你的文件上傳接口'
? ? ? ? ? ? ? ? , accept: 'file'
? ? ? ? ? ? ? ? , multiple: true
? ? ? ? ? ? ? ? , size: 30000
? ? ? ? ? ? ? ? , auto: false
? ? ? ? ? ? ? ? , bindAction: '#testListAction'
? ? ? ? ? ? ? ? ?, choose: function (obj) {
? ? ? ? ? ? ? ? ? ? ? var files = this.file = obj.pushFile(); //將每次選擇的文件追加到文件隊(duì)列
? ? ? ? ? ? ? ? ? ? ? console.log(uploadListIns);
? ? ? ? ? ? ? ? ? ? ? //讀取本地文件
? ? ? ? ? ? ? ? ? ? ? obj.preview(function (index, file, result) {
? ? ? ? ? ? ? ? ? ? ? var tr = $(['<tr id="upload-' + index + '">'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? , '<td>' + file.name + '</td>'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? , '<td>' + (file.size / 1014).toFixed(1) + 'kb</td>'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? , '<td>Wait to upload</td>'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? , '<td>'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? , '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重傳</button>'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? , '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">Delete</button>'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? , '</td>'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? , '</tr>'].join(''));
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //刪除
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? tr.find('.demo-delete').on('click', function () {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? delete files[index]; //刪除對(duì)應(yīng)的文件
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? tr.remove();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值,以免刪除后出現(xiàn)同名文件不可選
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //console.log(files);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? demoListView.append(tr);
? ? ? ? ? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? , done: function (res, index, upload) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (res.code == 0) //上傳成功
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? var tr = demoListView.find('tr#upload-' + index)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? , tds = tr.children();
? ? ? ? ? ? ? ? ? ? ? ? ? ? tds.eq(2).html('<span style="color: #5FB878;">Success</span>');
? ? ? ? ? ? ? ? ? ? ? ? ? ? tds.eq(3).html(''); //清空操作
? ? ? ? ? ? ? ? ? ? ? ? ? ? return delete this.files[index]; //刪除文件隊(duì)列已經(jīng)上傳成功的文件
? ? ? ? ? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? ? ? ? ? , error: function (index, upload) {
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? });
? ? ? ? ? ? })
</script>到這里的話其實(shí)就是從官網(wǎng)copy下來(lái)的哈哈哈,接下來(lái)的就是重點(diǎn)啦
2.后端部分
這里是controller部分
public async Task<IActionResult> UploadFiles(List<IFormFile> file)
? ? ? ? {
? ? ? ? ? ? EditorDataResult editorResult = new EditorDataResult();
? ? ? ? ? ? foreach (var formFile in file)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (formFile.Length > 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? FileInfo fi = new FileInfo(formFile.FileName);
? ? ? ? ? ? ? ? ? ? string ext = fi.Extension;
? ? ? ? ? ? ? ? ? ? var orgFileName = fi.Name;
? ? ? ? ? ? ? ? ? ? var newFileName = Guid.NewGuid() + ext;
? ? ? ? ? ? ? ? ? ? var uploads = Path.Combine(_hostingEnvironment.WebRootPath, "你想要上傳到文件夾");
? ? ? ? ? ? ? ? ? ? var filePath = Path.Combine(uploads, newFileName);
? ? ? ? ? ? ? ? ? ? using (var stream = new FileStream(filePath, FileMode.Create))
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? await formFile.CopyToAsync(stream);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? editorResult.code = 0;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? editorResult.code = 1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? JavaScriptSerializer jss = new JavaScriptSerializer();
? ? ?string data = jss.Serialize(editorResult);//轉(zhuǎn)換為Json格式!
? ? return Json(data);
}model部分 主要就是回調(diào)json數(shù)據(jù)給layui
namespace LayuiMvc.Common.Result
{
? ? public class EditorDataResult
? ? {
? ? ? ? public int code { get; set; }
? ? ? ? public string msg { get; set; }
? ? ? ? public Dictionary<string, string> data { get; set; }
? ? }
}到這邊基本上文件上傳已經(jīng)done了
上圖

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
asp.net計(jì)算每個(gè)頁(yè)面執(zhí)行時(shí)間的方法
這篇文章主要介紹了asp.net計(jì)算每個(gè)頁(yè)面執(zhí)行時(shí)間的方法,涉及asp.net操作時(shí)間的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
ASP.NET.4.5.1+MVC5.0設(shè)置系統(tǒng)角色與權(quán)限(一)
這篇文章主要介紹了ASP.NET.4.5.1+MVC5.0設(shè)置系統(tǒng)角色與權(quán)限的部分內(nèi)容,后續(xù)我們將繼續(xù)討論這個(gè)話題,希望小伙伴們喜歡。2015-01-01
ASP.NET Core使用JWT認(rèn)證授權(quán)的方法
這篇文章主要介紹了ASP.NET Core使用JWT認(rèn)證授權(quán)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
asp.net實(shí)現(xiàn)存儲(chǔ)和讀取數(shù)據(jù)庫(kù)圖片
這篇文章主要為大家詳細(xì)介紹了asp.net實(shí)現(xiàn)存儲(chǔ)和讀取數(shù)據(jù)庫(kù)圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
.net全局定時(shí)定期執(zhí)行某些操作在Global.asax中具體實(shí)現(xiàn)
全局定時(shí)定期執(zhí)行某些操作看起來(lái)是多么自動(dòng)化的一個(gè)問(wèn)題不過(guò)在.net的Global.asax文件中稍微配置即可實(shí)現(xiàn),詳細(xì)配置如下,感興趣的朋友可以參考下哈2013-04-04
Asp.net?MVC中的Http管道事件為什么要以Application_開(kāi)頭(原因解析)
在ASP.NET?MVC中,為了在API請(qǐng)求結(jié)束時(shí)釋放數(shù)據(jù)庫(kù)鏈接,避免連接池被爆掉,可以通過(guò)在Global.asax.cs文件中定義并實(shí)現(xiàn)Application_EndRequest方法來(lái)實(shí)現(xiàn),本文介紹Asp.net?MVC中的Http管道事件為什么要以Application_開(kāi)頭,感興趣的朋友一起看看吧2024-12-12
國(guó)產(chǎn)化中的?.NET?Core?操作達(dá)夢(mèng)數(shù)據(jù)庫(kù)DM8的兩種方式(操作詳解)
這篇文章主要介紹了國(guó)產(chǎn)化之?.NET?Core?操作達(dá)夢(mèng)數(shù)據(jù)庫(kù)DM8的兩種方式,這里提供兩種方式是傳統(tǒng)的DbHelperSQL方式和Dapper?方式,每種方式給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04

