.Net6.0+Vue3實(shí)現(xiàn)數(shù)據(jù)簡(jiǎn)易導(dǎo)入功能全過(guò)程
前言
在開(kāi)發(fā)的過(guò)程中,上傳文件或者導(dǎo)入數(shù)據(jù)是一件很常見(jiàn)的事情,導(dǎo)入數(shù)據(jù)可以有兩種方式:
- 前端上傳文件到后臺(tái),后臺(tái)讀取文件內(nèi)容,進(jìn)行驗(yàn)證再進(jìn)行存儲(chǔ)
- 前端讀取數(shù)據(jù),進(jìn)行數(shù)據(jù)驗(yàn)證,然后發(fā)送數(shù)據(jù)到后臺(tái)進(jìn)行存儲(chǔ)
這兩種方式需要根據(jù)不同的業(yè)務(wù)才進(jìn)行采用
這次用.Net6.0+Vue3來(lái)實(shí)現(xiàn)一個(gè)數(shù)據(jù)導(dǎo)入的功能
接下來(lái)分別用代碼來(lái)實(shí)現(xiàn)這兩種方式
1. 前端上傳文件到后臺(tái)進(jìn)行數(shù)據(jù)存儲(chǔ)
1.1編寫(xiě)文件上傳接口
[DisableRequestSizeLimit] [HttpPost] public IActionResult Upload() { var files = Request.Form.Files; long size = files.Sum(f => f.Length); string contentRootPath = AppContext.BaseDirectory; List<string> filenames = new List<string>(); foreach (IFormFile formFile in files) { if (formFile.Length > 0) { string fileExt = Path.GetExtension(formFile.FileName); long fileSize = formFile.Length; string newFileName = System.Guid.NewGuid().ToString() + fileExt; var filePath = contentRootPath + "/fileUpload/"; if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } using (var stream = new FileStream(filePath + newFileName, FileMode.Create)) { formFile.CopyTo(stream); } filenames.Add(newFileName); } } return Ok(filenames); }
這里只是上傳文件分了兩步走,第一步把文件上傳到服務(wù)器,第二步調(diào)用接口把返回的文件路徑發(fā)送給后臺(tái)進(jìn)行數(shù)據(jù)保存
1.2存儲(chǔ)上傳文件路徑,讀取數(shù)據(jù)并進(jìn)行存儲(chǔ)
/// <summary> /// 上傳文件數(shù)據(jù) /// </summary> /// <param name="uploadStuInfoInput"></param> /// <returns></returns> [HttpPut] public IActionResult Put(DataInput uploadStuInfoInput) { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "fileUpload", uploadStuInfoInput.filePath); if (!System.IO.File.Exists(filePath)) { return BadRequest("導(dǎo)入失敗,文件不存在!"); } var row = MiniExcelLibs.MiniExcel.Query<CompanyImportInput>(filePath).ToList(); companies.AddRange(row.Select(x => new Company { Name = x.名稱(chēng), Address = x.地址 })); return Ok("導(dǎo)入成功!"); }
1.3前端Vue建立創(chuàng)建列表數(shù)據(jù)頁(yè)面,包含表格功能及分頁(yè)功能
<el-table :data="state.tableData.data"> ? ? ? <el-table-column v-for="item in state.colunm" :prop="item.key" :key="item.key" :label="item.lable"> ? ? ? </el-table-column> ? ? </el-table> ?<div class='block flex justify-end' v-if='state.tableData.total > 0'> ? ? ? <el-pagination v-model:currentPage="state.searchInput.PageIndex" v-model:page-size="state.searchInput.PageSize" ? ? ? ? :page-sizes="[10, 50, 200, 1000]" layout="total, sizes, prev, pager, next, jumper" @size-change="getData" ? ? ? ? @current-change="getData" :total="state.tableData.total" /> ? ? </div>
1.4調(diào)用接口獲取表格數(shù)據(jù)方法
const getData = () => { ? ? ? axios.get('/Company', { params: state.searchInput }).then(function (response) { ? ? ? ? state.tableData = response.data; ? ? ? }) ? ? }
1.5后臺(tái)開(kāi)發(fā)數(shù)據(jù)返回接口
[HttpGet] public dynamic Get([FromQuery] SelectInput selectInput) { return new { total = companies.Count(), data = companies.Skip((selectInput.pageIndex - 1) * selectInput.pageSize).Take(selectInput.pageSize).ToList() }; }
1.6主頁(yè)面創(chuàng)建上傳文件組件并進(jìn)行引用
import FileUpload from '@/components/FileUpload.vue';
并綁定子頁(yè)面回調(diào)方法fileUploadchildClick
<FileUpload ref="fileUpload" @childClick="fileUploadchildClick" accept=".xlsx" title="上傳文件"></FileUpload>
1.7FleUpload頁(yè)面主要上傳文件到服務(wù)器,并回調(diào)父頁(yè)面存儲(chǔ)接口
<el-dialog :close-on-click-modal="false" v-model="state.dialogVisible" :title="title" width="40%"> ? ? <el-form :model='state.formData' label-width='130px' class='dialogForm'> ? ? ? <el-upload class='upload-demo' :limit="1" drag :accept='accept' :file-list='state.fileList' :show-file-list='true' ? ? ? ? :on-success='fileUploadEnd' :action='fileUploadUrl()'> ? ? ? ? <i class='el-icon-upload'></i> ? ? ? ? <div class='el-upload__text'>將文件拖到此處,或<em>點(diǎn)擊上傳</em></div> ? ? ? ? <div class='el-upload__tip'>請(qǐng)選擇({{ ?accept ?}})文件</div> ? ? ? </el-upload> ? ? ? <div> ? ? ? ? <el-form-item> ? ? ? ? ? <el-button type='primary' @click='submit'>導(dǎo)入</el-button> ? ? ? ? ? <el-button @click='cancel'>取消</el-button> ? ? ? ? </el-form-item> ? ? ? </div> ? ? </el-form> ? </el-dialog>
1.8這里的title,accept參數(shù)由父頁(yè)面?zhèn)髦颠^(guò)來(lái),可以進(jìn)行組件復(fù)用
選擇文件成功回調(diào)方法
const fileUploadEnd = (response, file) => { ? ? ? state.fileresponse = file.name; ? ? ? state.formData.filePath = response[0]; ? ? ? if (state.fileList.length > 0) { ? ? ? ? state.fileList.splice(0, 1); ? ? ? } ? ? }
1.9提交保存時(shí)回調(diào)父頁(yè)面存儲(chǔ)數(shù)據(jù)方法
const submit = () => { ? ? ? if (state.formData.filePath == '') { ? ? ? ? ElMessage.error('請(qǐng)選擇上傳的文件') ? ? ? ? return; ? ? ? } ? ? ? context.emit('childClick', state.formData) ? ? }
1.10父頁(yè)面方法調(diào)用接口進(jìn)行數(shù)據(jù)存儲(chǔ),存儲(chǔ)成功后關(guān)閉子頁(yè)面
const fileUploadchildClick = (e) => { ? ? ? axios.put('/Company', { ? ? ? ? filePath: e.filePath, ? ? ? }).then(function (response) { ? ? ? ? if (response.status == 200) { ? ? ? ? ? ElMessage.success(response.data); ? ? ? ? ? fileUpload.value.cancel(); ? ? ? ? ? getData(); ? ? ? ? } else { ? ? ? ? ? ElMessage.error(response.data) ? ? ? ? } ? ? ? }) ? ? }
1.11后臺(tái)數(shù)據(jù)存儲(chǔ)接口
/// <summary> /// 上傳文件數(shù)據(jù) /// </summary> /// <param name="uploadStuInfoInput"></param> /// <returns></returns> [HttpPut] public IActionResult Put(DataInput uploadStuInfoInput) { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "fileUpload", uploadStuInfoInput.filePath); if (!System.IO.File.Exists(filePath)) { return BadRequest("導(dǎo)入失敗,文件不存在!"); } var row = MiniExcelLibs.MiniExcel.Query<CompanyImportInput>(filePath).ToList(); companies.AddRange(row.Select(x => new Company { Name = x.名稱(chēng), Address = x.地址 })); return Ok("導(dǎo)入成功!"); }
2前端讀取數(shù)據(jù),發(fā)送讀取數(shù)據(jù)到后臺(tái)進(jìn)行數(shù)據(jù)存儲(chǔ)
2.1創(chuàng)建上傳數(shù)據(jù)組件并引用
import DataUpload from '@/components/DataUpload.vue';
并綁定子頁(yè)面回調(diào)方法dataUploadchildClick
<DataUpload ref="dataUpload" @childClick="dataUploadchildClick" accept=".xlsx" title="上傳數(shù)據(jù)"></DataUpload>
2.2DataUpload頁(yè)面主要讀取選擇文件數(shù)據(jù),并進(jìn)行展示
<el-dialog :close-on-click-modal="false" v-model="state.dialogVisible" :title="title" width="50%"> ? ? <el-upload class="upload-demo" :action='accept' drag :auto-upload="false" :on-change="uploadChange" :limit="1"> ? ? ? <i class="el-icon-upload"></i> ? ? ? <div class="el-upload__text">將文件拖到此處,或<em>點(diǎn)擊上傳</em></div> ? ? </el-upload> ? ? <div> ? ? ? <el-form-item> ? ? ? ? <el-button @click='submit'>確認(rèn)導(dǎo)入</el-button> ? ? ? </el-form-item> ? ? </div> ? ? <el-table :data="state.tableData.data"> ? ? ? <el-table-column v-for="item in state.colunm" :prop="item.key" :key="item.key" :label="item.lable"> ? ? ? </el-table-column> ? ? </el-table> ? ? <div class='block flex justify-end' v-if='state.tableData.total > 0'> ? ? ? <el-pagination v-model:currentPage="state.searchInput.PageIndex" v-model:page-size="state.searchInput.PageSize" ? ? ? ? :page-sizes="[10, 50, 200, 1000]" layout="total, sizes, prev, pager, next, jumper" @size-change="getData" ? ? ? ? @current-change="getData" :total="state.tableData.total" /> ? ? </div> ? </el-dialog>
2.3文件上傳成功方法,保存數(shù)據(jù)到臨時(shí)變量進(jìn)行分頁(yè)處理
const uploadChange = async (file) => { ? ? ? let dataBinary = await readFile(file.raw) ? ? ? let workBook = XLSX.read(dataBinary, { type: 'binary', cellDates: true }) ? ? ? let workSheet = workBook.Sheets[workBook.SheetNames[0]] ? ? ? let data: any = XLSX.utils.sheet_to_json(workSheet) ? ? ? let tHeader = state.colunm.map(obj => obj.lable) ? ? ? let filterVal = state.colunm.map(obj => obj.key) ? ? ? tHeader.map(val => filterVal.map(obj => val[obj])) ? ? ? const tempData: any = []; ? ? ? data.forEach((value) => { ? ? ? ? const ob = {}; ? ? ? ? tHeader.forEach((item, index) => { ? ? ? ? ? ob[filterVal[index]] = value[item].toString(); ? ? ? ? }) ? ? ? ? tempData.push(ob); ? ? ? }) ? ? ? state.tempTableData = tempData; ? ? ? getData(); ? ? }
2.4數(shù)據(jù)綁定到表格上,這里需要通過(guò)當(dāng)前選擇頁(yè)碼及頁(yè)面顯示數(shù)量處理需要綁定到表格上的數(shù)據(jù)
const getData = () => { ? ? ? const tempData: any = []; ? ? ? state.tempTableData.forEach((value, index) => { ? ? ? ? if (index >= ((state.searchInput.PageIndex - 1) * state.searchInput.PageSize) && index < ((state.searchInput.PageIndex) * state.searchInput.PageSize)) { ? ? ? ? ? tempData.push(value); ? ? ? ? } ? ? ? }); ? ? ? state.tableData.data = tempData; ? ? ? state.tableData.total = state.tempTableData.length; ? ? }
2.5點(diǎn)擊確認(rèn)導(dǎo)入按鈕回調(diào)父頁(yè)面方法進(jìn)行數(shù)據(jù)保存
const submit = () => { ? ? ? context.emit('childClick', state.tempTableData) ? ? }
2.6父頁(yè)面方法調(diào)用接口進(jìn)行數(shù)據(jù)存儲(chǔ),存儲(chǔ)成功后關(guān)閉子頁(yè)面
const dataUploadchildClick = (data) => { ? ? ? axios.post('/Company', data) ? ? ? ? .then(function (response) { ? ? ? ? ? if (response.status == 200) { ? ? ? ? ? ? ElMessage.success(response.data); ? ? ? ? ? ? dataUpload.value.cancel(); ? ? ? ? ? ? getData(); ? ? ? ? ? } else { ? ? ? ? ? ? ElMessage.error(response.data) ? ? ? ? ? } ? ? ? ? }) ? ? }
2.7后臺(tái)數(shù)據(jù)存儲(chǔ)接口
/// 上傳數(shù)據(jù) /// </summary> /// <param name="uploadStuInfoInput"></param> /// <returns></returns> [HttpPost] public IActionResult Post(List<Company> companiesInput) { companies.AddRange(companiesInput); return Ok("保存成功!"); }
最后關(guān)于這個(gè)數(shù)據(jù)導(dǎo)入的功能就完成了,代碼中有很多得偽代碼,而且很多功能還待完善,后續(xù)再進(jìn)行補(bǔ)充
附上git地址:https://gitee.com/wyf854861085/file-upload.git
Git演示圖:
總結(jié)
到此這篇關(guān)于.Net6.0+Vue3實(shí)現(xiàn)數(shù)據(jù)簡(jiǎn)易導(dǎo)入功能的文章就介紹到這了,更多相關(guān).Net Vue3數(shù)據(jù)導(dǎo)入功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 使用mongovue把sqlserver數(shù)據(jù)導(dǎo)入mongodb的步驟
- 如何使用vue實(shí)現(xiàn)前端導(dǎo)入excel數(shù)據(jù)
- Vue實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出導(dǎo)入實(shí)戰(zhàn)案例
- Vue3實(shí)現(xiàn)動(dòng)態(tài)導(dǎo)入Excel表格數(shù)據(jù)的方法詳解
- 使用VUE+SpringBoot+EasyExcel?整合導(dǎo)入導(dǎo)出數(shù)據(jù)的教程詳解
- Vue導(dǎo)入excel表,導(dǎo)入失敗的數(shù)據(jù)自動(dòng)下載
- Vue實(shí)現(xiàn)數(shù)據(jù)導(dǎo)入的四種方法(resource、Axios、Fetch、Excel導(dǎo)入)
相關(guān)文章
ASP.NET 動(dòng)態(tài)寫(xiě)入服務(wù)器端控件
使用Asp.net進(jìn)行開(kāi)發(fā)時(shí),因?yàn)槟承┬枨笤?,需要在?yè)面中動(dòng)態(tài)添加控件。當(dāng)然,這些控件可以是普通的html標(biāo)簽,也可以是Asp.net獨(dú)有的服務(wù)器端控件。2009-04-04.Net Core下HTTP請(qǐng)求IHttpClientFactory示例詳解
這篇文章主要給大家介紹了關(guān)于.Net Core下HTTP請(qǐng)求IHttpClientFactory的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用.Net Core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09利用FastReport傳遞圖片參數(shù)在報(bào)表上展示簽名信息的實(shí)現(xiàn)方法
這篇文章主要介紹了利用FastReport傳遞圖片參數(shù)在報(bào)表上展示簽名信息,其中主要注意的是,我們傳遞的圖片數(shù)據(jù)需要采用Base64String的格式才能正常傳遞和展示,本文通過(guò)圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10Asp.net實(shí)現(xiàn)選擇性的保留DataTable中的列
選擇性的保留DataTable中的列(移除列/保留列不移除/移除不需要的列),很多新手朋友們都想實(shí)現(xiàn)這樣的功能,本文總結(jié)了一些可行方法,感興趣的朋友可以了解下哦2013-01-01淺談.Net并行計(jì)算之?dāng)?shù)據(jù)并行
這篇文章主要介紹了.Net并行計(jì)算之?dāng)?shù)據(jù)并行,有需要的朋友可以參考一下2013-12-12ASP.NET?Core管理應(yīng)用程序狀態(tài)
這篇文章介紹了ASP.NET?Core管理應(yīng)用程序狀態(tài)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04ASP.NET MVC錯(cuò)誤處理的對(duì)應(yīng)解決方法
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC錯(cuò)誤處理的對(duì)應(yīng)解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03