当前位置:   article > 正文

vue 文件上传前后端_vue上传文件到后端

vue上传文件到后端

一、前端vue3

  1. <el-dialog title="编辑" v-model="editFormVisible">
  2. <el-form ref="editForm" :model="editForm" :rules="rules" status-icon label-width="90px">
  3. <el-form-item label="选择文件:" prop="">
  4. <el-upload v-model:file-list="editForm.fileList" class="upload-demo"
  5. :action="fileBaseUrl" :headers="uploadHeaders" :on-change="handleChange">
  6. <el-button type="primary">Click to upload</el-button>
  7. <template #tip>
  8. <div class="el-upload__tip">
  9. jpg/png files with a size less than 500kb
  10. </div>
  11. </template>
  12. </el-upload>
  13. </el-form-item>
  14. </el-form>
  15. <template #footer>
  16. <span class="dialog-footer">
  17. <el-button @click="editFormVisible = false">取 消</el-button>
  18. <el-button type="primary" @click="saveEdit" :loading="saveBtnLoading">确 定</el-button>
  19. </span>
  20. </template>
  21. </el-dialog>

 fileBaseUrl:上传后台接口地址

uploadHeaders:身份验证token

二、后台C#接口

  1. [HttpPost("upload")]
  2. [ProducesResponseType(typeof(SucceedResponse<bool>), StatusCodes.Status200OK)]
  3. public virtual async Task<IActionResult> Upload([FromForm] IFormCollection formCollection)
  4. {
  5. if (formCollection == null || formCollection.Files.Count <= 0)
  6. {
  7. return Ok(new ErrorResponse<string> { Data = "请选择文件" });
  8. }
  9. var fileCollection = (FormFileCollection)formCollection.Files;
  10. foreach (IFormFile file in fileCollection)
  11. {
  12. var fileName = file.FileName;
  13. var ext = System.IO.Path.GetExtension(fileName);
  14. using (var memory = new MemoryStream())
  15. {
  16. file.CopyTo(memory);
  17. var buffer = memory.ToArray();
  18. }
  19. }
  20. return Ok(new SucceedResponse<bool> { Data = true });
  21. }

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号