Blogger Information
Blog 87
fans 1
comment 0
visits 58344
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
前后端分离 - 文件上传
阿杰
Original
506 people have browsed it

后端接口使用tp6

  • config文件夹下,配置filesystem.php

  • 单文件上传

  • 多文件上传

前端使用vue3

  • 通过input标签来上传文件

  • 单文件上传

  1. <input type="file" @change="handleFile">
  2. <!-- 存放图片 -->
  3. <img v-if="curImgUrl!=''" :src="baseImg+curImgUrl" alt="" srcset="" style="width:200px;height:auto;">
  4. <button @click="toUpload">上传</button>
  1. const handleFile = ()=>{
  2. // console.log(window.event.target.files);
  3. let filePaths = window.event.target.files[0];
  4. let obj = {file:filePaths};
  5. fileUrl.value = obj;
  6. }
  7. const toUpload = ()=>{
  8. // 创建一个表单数据
  9. var data = new FormData();
  10. data.append("file",fileUrl.value.file);
  11. uploads(data).then((res)=>{
  12. console.log(res);
  13. alert(res.message);
  14. if(res.code==1){
  15. curImgUrl.value = res.data.img;
  16. }
  17. })
  18. }

  • 多文件上传
  1. <!-- 多文件上传 -->
  2. <input type="file" id="back" ref="backfile" multiple @change="handleFile2" />
  3. <div class="btCont">
  4. <button class="myBt" @click="toUpload2">上传</button>
  5. </div>
  1. const handleFile2 = ()=>{
  2. let filePaths = window.event.target.files;
  3. //清空原有缩略图
  4. if (filePaths.length === 0) {
  5. //未选择,则返回
  6. return
  7. } else {
  8. //清空数组中原有图片
  9. selFiles.value.length = 0;
  10. }
  11. //把新选中的图片加入数组
  12. for( var i=0;i<filePaths.length; i++ ){
  13. let file = filePaths[i];
  14. let one = {
  15. file:file,
  16. }
  17. selFiles.value.push(one);
  18. }
  19. }
  20. const toUpload2 = ()=>{
  21. // 创建一个表单数据
  22. var data = new FormData();
  23. //遍历文件,添加到form
  24. for( var i=0;i<selFiles.value.length; i++ ){
  25. let fileOne = selFiles.value[i].file;
  26. data.append("file[]",fileOne);
  27. }
  28. uploadss(data).then((res)=>{
  29. console.log(res);
  30. alert(res.message);
  31. })
  32. }

存储地址

  • 根据filesystem.php文件设置的地址存放上传的文件

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post