首页 > 其他分享 >laravel:多图片上传(10.27.0/前端vue)

laravel:多图片上传(10.27.0/前端vue)

时间:2023-10-30 09:12:13浏览次数:38  
标签:laravel vue selFiles 10.27 data let file div 上传

一,相关文档

https://learnku.com/docs/laravel/10.x/filesystem/14865#481e03

二,前端vue代码

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 <template>   <div>     请选择上传幻灯图片:     <input type="file" id="back" ref="backfile" multiple  @change="handleFile" />     <div id="imglist" style="width:100%;">       <div v-for="(item,i) in selFiles" :key="i" style="float:left;margin-left: 20px;margin-top: 20px;height:150px;position:relative;">         <img :src="item.fileimg"  style="height:150px;"/>         <div @click="delqueue(i)" style="position:absolute;width:30px;height:30px;line-height:30px;border-radius:15px;top:-15px;right:-15px;background:#ff0000;">x</div>       </div>     </div>     <input type="button" value="上传" @click="upload" />   </div> </template> <script> import {ref} from "vue" import axios from 'axios'; export default {   name: "MultiUploadImg",   setup() {     //选中的图片文件,保存在数组中     const selFiles = ref([]);     //选中图片后的处理     const handleFile = () => {       let filePaths = window.event.target.files;       //清空原有缩略图       if (filePaths.length === 0) {         //未选择,则返回         return       } else {         //清空数组中原有图片         selFiles.value.length = 0;       }       //把新选中的图片加入数组       for( var i=0;i<filePaths.length; i++ ){         let file = filePaths[i];         let one = {           fileimg:URL.createObjectURL(file),  //预览用           file:file,         }         selFiles.value.push(one);       }     }     //从上传数组中删除     const delqueue = (index) => {       if (confirm("确定从上传队列中删除当前图片?")) {         selFiles.value.splice(index,1);       }     }     //上传     const upload = () => {       //判断是否选中文件       var file = selFiles.value[0].file;       if (typeof(file) === "undefined") {         alert("请在上传前先选中文件!");         return;       }       // 创建一个表单数据       var data = new FormData();       //遍历文件,添加到form       for( var i=0;i<selFiles.value.length; i++ ){         let fileOne = selFiles.value[i].file;         data.append("img[]",fileOne);       }       let url = "/api/news/multiimgadd";       //axios上传文件       axios({         method:"post",         url:url,         data:data,         headers:{'Content-Type': 'multipart/form-data'},       }).then((res) => {             if (res.data.code == 0) {               let data = res.data.data;               let imageUrl = data.urls;               alert("success:image:"+imageUrl);             } else {               alert("error:"+res.data.msg);             }           }       ).catch(err=>{         alert('网络错误:'+err.message);       });     }     return {       handleFile,       selFiles,       delqueue,       upload,     }   } } </script> <style scoped> </style>

三,后端php代码:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 //多图片上传 public function multiImgAdd(Request $request) {   if ($request->hasFile('img')) {       $str = "";       //遍历文件       foreach ($request->file('img') as $index=> $e) {             //得到扩展名             $ext = $e->getClientOriginalExtension();             $filename = time().rand().".".$ext;             $baseDir = "/var/www/html/digImage";             //保存图片到指定目录             $e->move($baseDir,$filename);            //返回url             $url = "http://192.168.219.6/digImage/".$filename;             if ($str == '') {                 $str = $url;             } else {                 $str .= ";".$url;             }         }         return Result::Success(['urls'=>$str]);     }else{         // 回到上一个页面         //return back();         return Result::ErrorCode(10024,'图片文件未上传');     } }

说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/10/29/laravel-duo-tu-pian-shang-chuan-10-27-qian-duan-vue/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: [email protected]

四,测试效果:

五,查看laravel框架的版本:

liuhongdi@lhdpc:/data/laravel/dignews$ php artisan --version
Laravel Framework 10.27.0

标签:laravel,vue,selFiles,10.27,data,let,file,div,上传
From: https://www.cnblogs.com/architectforest/p/17797025.html

相关文章

  • laravel:单图片上传(10.27.0/前端vue)
    一,相关文档https://learnku.com/docs/laravel/10.x/filesystem/14865#481e03二,前端vue代码123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657......
  • Vuex状态管理常见的几种使用功能场景
    Vuex是一个专为Vue.js应用程序开发的状态管理模式。用于集中管理应用程序的所有组件之间共享的状态,确保状态的一致性和可预测性。使用Vuex的一般步骤如下:1:安装Vuex:使用npm或yarn安装Vuex库。npminstallvuex2:创建VuexStore:在Vue应用程序中创建一个Vuex的store实例,包含了应用程序......
  • vue的性能优化
    1、v-if和v-show却别场景使用2、computed和watch区别场景使用3、v-for循环需要添加key,并且v-for不能和v-if同时使用4、文件懒加载5、路由懒加载6、第三方插件的按需加载7、事件销毁,计时器销毁(如果有)......
  • 基于Vue2+elementUI的二手书管理系统-计算机毕业设计源码+LW文档
    摘 要本设计完成了基于Vue2+elementUI的二手书管理系统的设计与实现。现代移动化网络发展下,不同于以往的短信、邮件、收音机传递信息,网页是向用户传输信息的主要媒介之一。书籍也是向人们传递信息和知识的媒介,如今书籍印刷和出版的快速发展,以及社会文化水平的进步,越来越多的读书......
  • 初看vue3源码
    因为工作的原因又回到了vue的领域,需要加深对vue和vue生态的了解也许平时比较多人手机看别人解析怎么看vue源码的,自己动手看vue源码的还是比较少,这次我想自己动手看看首先吧代码获取到本地跑起来vue仓库地址https://github.com/vuejs/vue开发环境搭建指南https://github.com/......
  • springboot+vue2+element学生信息管理系统
    效果:  .vue<template><div><el-containerstyle="height:700px;border:1pxsolid#eee"><el-headerstyle="font-size:40px;background-color:rgb(238,241,246)">学生管理</el-header&......
  • vue sass
    1.安装:[email protected]注:①.sass-loader依赖于node-sass(1).报错:Modulebuildfailed:TypeError:this.getResolveisnotafunctionatObject.loader(path/node_modules/sass-loader/dist/index.js:52:26)2.注释:①./*......
  • Vue进阶(贰零玖):@click.stop与@click.prevent应用讲解
    一、@click.stop问题:父元素中添加了一个click事件,其下面的子元素中也添加了click事件,此时,我想点击子元素获取子元素的点击事件,但却触发的是父元素的事件:<viewclass="footer-box"@click="clickCard"> <view@click="footerClick('喜欢')"><textclass="footer-box__i......
  • laravel:.env中APP_KEY的用途(10.27.0)
    一,APP_KEY的作用:1,用途:它作为网站的密钥使用,用来保护网站的安全主要用于加密cookie2,生成APP_KEY:生成前:APP_KEY=生成命令:[root@imgdignews]#/usr/local/soft/php8.2.5/bin/php  artisankey:generate   INFO  Applicationkeysetsuccessfully.生成后......
  • laravel:维护模式:上线/下线(10.27.0)
    一,相关文档:https://learnku.com/docs/laravel/10.x/configuration/14836#972c4c二,用artisan工具实现上线下线1,下线,进入维护模式[root@imgdignews]#/usr/local/soft/php8.2.5/bin/phpartisandown   INFO  Applicationisnowinmaintenancemode.2,上线,关......