elementui+vue+axios實(shí)現(xiàn)文件上傳本地服務(wù)器
本文實(shí)例為大家分享了elementui+vue+axios實(shí)現(xiàn)文件上傳本地服務(wù)器的具體代碼,供大家參考,具體內(nèi)容如下
文件上傳的原理

加入文件上傳的依賴(lài)
<!--文件上傳的依賴(lài)--> ? ? <dependency> ? ? ? <groupId>commons-fileupload</groupId> ? ? ? <artifactId>commons-fileupload</artifactId> ? ? ? <version>1.4</version> </dependency>
創(chuàng)建一個(gè)web頁(yè)面
<%-- ? Created by IntelliJ IDEA. ? User: 王磊 ? Date: 2022/6/9 ? Time: 19:19 ? To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> ? ? <title>Title</title> </head> <body> <%-- ? ? method: 提交的方式 ?文件的上傳必須是post的提交 ? ? enctype:默認(rèn)為application/x-www-form-urlencoded ?表示提交表單數(shù)據(jù) ? ? ? ? ? ? multipart/form-data 可以包含文件數(shù)據(jù) ? ? ? ? ? ? file 文件 ? ? ? ? ? ? input的類(lèi)型必須為 file 類(lèi)型 ?而且必須有name 屬性 --%> <form method="post" action="upload01" enctype="multipart/form-data"> ? ? <input type="file" name="myfile"/><br> ? ? <input type="submit" value="提交"> </form> </body> </html>
在springmvc中配置文件上傳解析器
?<!-- ? ? ?id的名稱(chēng)必須叫multipartResolver ? ? ?--> ? ? ?<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> ? ? ? ? ? <!--這里的單位為字節(jié)10M*1024K*1024--> ? ? ? ? ? <property name="maxUploadSize" value="10485760"/> ? ? ?</bean>
創(chuàng)建upload01接口方法
package com.wzl.controller;/*
?* @author ? ? : wzl
?* @date ? ? ? : 2022/6/9 19:14
?* @description: some description
?*/
?
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
?
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.UUID;
?
@Controller
public class pictureController {
? ? @RequestMapping("/upload01")
? ? public String upload01(MultipartFile myfile, HttpServletRequest request) throws Exception{
?
? ? ? ? //(1)得到本地服務(wù)目錄的地址
? ? ? ? String path = request.getSession().getServletContext().getRealPath("upload");
? ? ? ? //(2)判斷該目錄是否存在
? ? ? ? File file=new File(path);
? ? ? ? if(!file.exists()){
? ? ? ? ? ? file.mkdirs();
? ? ? ? }
? ? ? ? //(3)//把myfile保存到本地服務(wù)中某個(gè)文件夾下。 缺點(diǎn):文件的名稱(chēng)
? ? ? ? String filename= UUID.randomUUID().toString().replace("-","")+myfile.getOriginalFilename();
? ? ? ? File target=new File(path+"/"+filename);
? ? ? ? myfile.transferTo(target); //把myfile轉(zhuǎn)移到目標(biāo)目錄下
? ? ? ? return "";
? ? }
}加入web后
<%--
? Created by IntelliJ IDEA.
? User: 王磊
? Date: 2022/6/9
? Time: 20:35
? To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
? ? <title>Title</title>
? ? <!--引入element的css樣式-->
? ? <link type="text/css" rel="stylesheet" href="css/index.css">
? ? <!--引入vue的js文件-->
? ? <script type="text/javascript" src="js/vue.js"></script>
? ? <script type="text/javascript" src="js/qs.min.js"></script>
? ? <script type="text/javascript" src="js/axios.min.js"></script>
? ? <!--element的js文件-->
? ? <script type="text/javascript" src="js/index.js"></script>
</head>
<body>
<div id="app">
? ? <%--action:文件上傳的路徑--%>
? ? <el-upload
? ? ? ? ? ? class="avatar-uploader"
? ? ? ? ? ? action="/upload02"
? ? ? ? ? ? :show-file-list="false"
? ? ? ? ? ? :on-success="handleAvatarSuccess"
? ? ? ? ? ? :before-upload="beforeAvatarUpload">
? ? ? ? <img v-if="imageUrl" :src="imageUrl" class="avatar">
? ? ? ? <i v-else class="el-icon-plus avatar-uploader-icon"></i>
? ? </el-upload>
</div>
</body>
<script>
? ? var app=new Vue({
? ? ? ? el:"#app",
? ? ? ? data:{
? ? ? ? ? ? imageUrl:"",
? ? ? ? },
? ? ? ? methods:{
? ? ? ? ? ? //上傳成功后觸發(fā)的方法
? ? ? ? ? ? handleAvatarSuccess(res, file) {
? ? ? ? ? ? ? ? this.imageUrl=res.data;
? ? ? ? ? ? },
? ? ? ? ? ? //上傳前觸發(fā)的方法
? ? ? ? ? ? beforeAvatarUpload(file) {
? ? ? ? ? ? ? ? const isJPG = file.type === 'image/jpeg';
? ? ? ? ? ? ? ? const isPNG = file.type === 'image/png';
? ? ? ? ? ? ? ? const isLt2M = file.size / 1024 / 1024 < 2;
? ? ? ? ? ? ? ? if (!isJPG) {
? ? ? ? ? ? ? ? ? ? this.$message.error('上傳頭像圖片只能是 JPG 格式!');
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (!isLt2M) {
? ? ? ? ? ? ? ? ? ? this.$message.error('上傳頭像圖片大小不能超過(guò) 2MB!');
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return isJPG && isLt2M;
? ? ? ? ? ? }
? ? ? ? }
? ? })
</script>
?
<style>
? ? .avatar-uploader .el-upload {
? ? ? ? border: 1px dashed #d9d9d9;
? ? ? ? border-radius: 6px;
? ? ? ? cursor: pointer;
? ? ? ? position: relative;
? ? ? ? overflow: hidden;
? ? }
? ? .avatar-uploader .el-upload:hover {
? ? ? ? border-color: #409EFF;
? ? }
? ? .avatar-uploader-icon {
? ? ? ? font-size: 28px;
? ? ? ? color: #8c939d;
? ? ? ? width: 178px;
? ? ? ? height: 178px;
? ? ? ? line-height: 178px;
? ? ? ? text-align: center;
? ? }
? ? .avatar {
? ? ? ? width: 178px;
? ? ? ? height: 178px;
? ? ? ? display: block;
? ? }
</style>
</html>后臺(tái)的接口
package com.wzl.controller;/* ?* @author ? ? : wzl ?* @date ? ? ? : 2022/6/9 20:40 ?* @description: some description ?*/ ? import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; ? import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.UUID; ? @Controller
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue-router的鉤子函數(shù)用法實(shí)例分析
這篇文章主要介紹了vue-router的鉤子函數(shù)用法,結(jié)合實(shí)例形式分析了vue路由鉤子分類(lèi)及vue-router鉤子函數(shù)相關(guān)使用技巧,需要的朋友可以參考下2019-10-10
vue3?圖片懶加載的兩種方式、IntersectionObserver和useIntersectionObserve
這篇文章主要介紹了vue3?圖片懶加載的兩種方式、IntersectionObserver和useIntersectionObserver實(shí)例詳解,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03
詳解Vue如何實(shí)現(xiàn)響應(yīng)式布局
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)響應(yīng)式布局的兩種方法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起了解一下2023-12-12
vue 的keep-alive緩存功能的實(shí)現(xiàn)
本篇文章主要介紹了vue 的keep-alive緩存功能的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
vue?長(zhǎng)列表數(shù)據(jù)刷新的實(shí)現(xiàn)及思考
這篇文章主要為大家介紹了vue?長(zhǎng)列表數(shù)據(jù)刷新的實(shí)現(xiàn)及思考,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
uniapp仿微信聊天界面效果實(shí)例(vue3組合式版本)
這篇文章主要介紹了uniapp仿微信聊天界面的相關(guān)資料,這里提及了一個(gè)時(shí)間工具包timeMethod.js,該工具包可能提供了一系列時(shí)間處理的功能,如格式化日期、計(jì)算時(shí)間差等,以便在消息格式中正確展示時(shí)間信息,使用此類(lèi)工具包可以大大提高開(kāi)發(fā)效率,需要的朋友可以參考下2024-10-10
vue axios數(shù)據(jù)請(qǐng)求get、post方法及實(shí)例詳解
axios是一個(gè)基于Promise,同時(shí)支持瀏覽器端和Node.js的HTTP庫(kù),常用于Ajax請(qǐng)求。這篇文章主要介紹了vue axios數(shù)據(jù)請(qǐng)求get、post方法的使用 ,需要的朋友可以參考下2018-09-09

