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

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

时间:2023-10-30 09:04:44浏览次数:34  
标签:laravel vue selFiles 10.27 url 上传 file div data

一,相关文档

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 95 96 97 <template>   <div>     <div style="width:800px;margin: auto;display: flex;flex-direction: column;">       请选择上传图片:       <input type="file" id="back" ref="backfile" @change="handleFile" /><br/>       <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>       </div>       <div>         <input type="button" value="上传" @click="upload" />       </div>     </div>   </div> </template> <script> import {ref} from "vue"; import axios from 'axios'; export default {   name: "ImgUpload",   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 upload = () => {       //判断是否选中文件       if (selFiles.value.length == 0) {         alert("请在上传前先选中文件!");         return;       }       var file = selFiles.value[0].file;       if (typeof(file) === "undefined") {         alert("请在上传前先选中文件!");         return;       }       // 创建一个表单数据       var data = new FormData();       //遍历文件并上传       for( var i=0;i<selFiles.value.length; i++ ){         let fileOne = selFiles.value[i].file;         data.append("img",fileOne);       }       data.append("userName","laoliu");       let url = "/api/news/imgadd";       //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;               console.log(data);               let imageUrl = data.url;               alert("success:image:"+imageUrl);             } else {               alert("error:"+res.data.msg);             }           }       ).catch(err=>{         alert('网络错误:'+err.message);       });     }     return {       selFiles,       handleFile,       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 public function imgadd(Request $request) {     // 判断文件是否上传     // 实例     if ($request->hasFile('img')) {         // 获取后缀名         $ext=$request->file('img')->getClientOriginalExtension();         // 新的文件名         $newFile=time().rand().".".$ext;         //保存到目录         $baseDir = "/var/www/html/digImage";         // 上传文件操作         $request->file('img')->move($baseDir,$newFile);         //得到图片的url         $url = "http://192.168.219.6/digImage/".$newFile;         //返回         return Result::Success(['url'=>$url]);     }else{         // 回到上一个页面         //return back();         return Result::ErrorCode(10024,'图片文件未上传');     } }

说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/10/29/laravel-dan-tu-pian-shang-chuan-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,url,上传,file,div,data
From: https://www.cnblogs.com/architectforest/p/17796999.html

相关文章

  • 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,上线,关......
  • vue+odoo前后端搭建
    项目所需前端用vue的element组件后端用odoo 1、为什么选用element(饿了么)源自vuevue国人开发文档完善便于自学关键是我以前接触过比react上手快一开始的路线是想直接在template里面引入vue的js在xml混写,结果总是报错,这两天一直在想办法解决报错最后没解决今晚上只......