成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

.Net6.0+Vue3實(shí)現(xiàn)數(shù)據(jù)簡(jiǎn)易導(dǎo)入功能全過(guò)程

瀏覽:268日期:2022-06-08 11:26:27
目錄
  • 前言
  • 1. 前端上傳文件到后臺(tái)進(jìn)行數(shù)據(jù)存儲(chǔ)
    • 1.1編寫(xiě)文件上傳接口
    • 1.2存儲(chǔ)上傳文件路徑,讀取數(shù)據(jù)并進(jìn)行存儲(chǔ)
    • 1.3前端Vue建立創(chuàng)建列表數(shù)據(jù)頁(yè)面,包含表格功能及分頁(yè)功能
    • 1.4調(diào)用接口獲取表格數(shù)據(jù)方法
  • 1.5后臺(tái)開(kāi)發(fā)數(shù)據(jù)返回接口
    • 1.6主頁(yè)面創(chuàng)建上傳文件組件并進(jìn)行引用
    • 1.7FleUpload頁(yè)面主要上傳文件到服務(wù)器,并回調(diào)父頁(yè)面存儲(chǔ)接口
    • 1.8這里的title,accept參數(shù)由父頁(yè)面?zhèn)髦颠^(guò)來(lái),可以進(jìn)行組件復(fù)用
    • 1.9提交保存時(shí)回調(diào)父頁(yè)面存儲(chǔ)數(shù)據(jù)方法
    • 1.10父頁(yè)面方法調(diào)用接口進(jìn)行數(shù)據(jù)存儲(chǔ),存儲(chǔ)成功后關(guān)閉子頁(yè)面
    • 1.11后臺(tái)數(shù)據(jù)存儲(chǔ)接口
  • 2前端讀取數(shù)據(jù),發(fā)送讀取數(shù)據(jù)到后臺(tái)進(jìn)行數(shù)據(jù)存儲(chǔ)
    • 2.1創(chuàng)建上傳數(shù)據(jù)組件并引用
    • 2.2DataUpload頁(yè)面主要讀取選擇文件數(shù)據(jù),并進(jìn)行展示
    • 2.3文件上傳成功方法,保存數(shù)據(jù)到臨時(shí)變量進(jìn)行分頁(yè)處理
    • 2.4數(shù)據(jù)綁定到表格上,這里需要通過(guò)當(dāng)前選擇頁(yè)碼及頁(yè)面顯示數(shù)量處理需要綁定到表格上的數(shù)據(jù)
    • 2.5點(diǎn)擊確認(rèn)導(dǎo)入按鈕回調(diào)父頁(yè)面方法進(jìn)行數(shù)據(jù)保存
    • 2.6父頁(yè)面方法調(diào)用接口進(jìn)行數(shù)據(jù)存儲(chǔ),存儲(chǔ)成功后關(guān)閉子頁(yè)面
    • 2.7后臺(tái)數(shù)據(jù)存儲(chǔ)接口
  • 總結(jié)

    前言

    在開(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.名稱, 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.名稱, 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 :action="accept" drag :auto-upload="false" :on-change="uploadChange" :limit="1">? ? ? <i></i>? ? ? <div>將文件拖到此處,或<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)文章希望大家以后多多支持!

    標(biāo)簽: ASP.NET