最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

elementui+vue+axios實現(xiàn)文件上傳本地服務(wù)器

 更新時間:2022年08月15日 17:18:01   作者:阿磊學(xué)不會001  
這篇文章主要為大家詳細介紹了elementui+vue+axios實現(xiàn)文件上傳本地服務(wù)器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

文件上傳的原理

加入文件上傳的依賴

<!--文件上傳的依賴-->
? ? <dependency>
? ? ? <groupId>commons-fileupload</groupId>
? ? ? <artifactId>commons-fileupload</artifactId>
? ? ? <version>1.4</version>
</dependency>

創(chuàng)建一個web頁面

<%--
? 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:默認為application/x-www-form-urlencoded ?表示提交表單數(shù)據(jù)
? ? ? ? ? ? multipart/form-data 可以包含文件數(shù)據(jù)
? ? ? ? ? ? file 文件
? ? ? ? ? ? input的類型必須為 file 類型 ?而且必須有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的名稱必須叫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ù)中某個文件夾下。 缺點:文件的名稱
? ? ? ? String filename= UUID.randomUUID().toString().replace("-","")+myfile.getOriginalFilename();
? ? ? ? File target=new File(path+"/"+filename);
? ? ? ? myfile.transferTo(target); //把myfile轉(zhuǎn)移到目標目錄下
? ? ? ? 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('上傳頭像圖片大小不能超過 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>

后臺的接口

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue-router的鉤子函數(shù)用法實例分析

    vue-router的鉤子函數(shù)用法實例分析

    這篇文章主要介紹了vue-router的鉤子函數(shù)用法,結(jié)合實例形式分析了vue路由鉤子分類及vue-router鉤子函數(shù)相關(guān)使用技巧,需要的朋友可以參考下
    2019-10-10
  • 用vue快速開發(fā)app的腳手架工具

    用vue快速開發(fā)app的腳手架工具

    這篇文章主要介紹了用vue快速開發(fā)app的腳手架工具,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • vue3?圖片懶加載的兩種方式、IntersectionObserver和useIntersectionObserver實例詳解

    vue3?圖片懶加載的兩種方式、IntersectionObserver和useIntersectionObserve

    這篇文章主要介紹了vue3?圖片懶加載的兩種方式、IntersectionObserver和useIntersectionObserver實例詳解,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2023-03-03
  • 詳解Vue如何實現(xiàn)響應(yīng)式布局

    詳解Vue如何實現(xiàn)響應(yīng)式布局

    這篇文章主要為大家詳細介紹了Vue實現(xiàn)響應(yīng)式布局的兩種方法,文中的示例代碼講解詳細,具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-12-12
  • vue 的keep-alive緩存功能的實現(xiàn)

    vue 的keep-alive緩存功能的實現(xiàn)

    本篇文章主要介紹了vue 的keep-alive緩存功能的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • VUE中如何實現(xiàn)阻止事件冒泡

    VUE中如何實現(xiàn)阻止事件冒泡

    這篇文章主要介紹了VUE中如何實現(xiàn)阻止事件冒泡,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • vue?長列表數(shù)據(jù)刷新的實現(xiàn)及思考

    vue?長列表數(shù)據(jù)刷新的實現(xiàn)及思考

    這篇文章主要為大家介紹了vue?長列表數(shù)據(jù)刷新的實現(xiàn)及思考,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • uniapp仿微信聊天界面效果實例(vue3組合式版本)

    uniapp仿微信聊天界面效果實例(vue3組合式版本)

    這篇文章主要介紹了uniapp仿微信聊天界面的相關(guān)資料,這里提及了一個時間工具包timeMethod.js,該工具包可能提供了一系列時間處理的功能,如格式化日期、計算時間差等,以便在消息格式中正確展示時間信息,使用此類工具包可以大大提高開發(fā)效率,需要的朋友可以參考下
    2024-10-10
  • vue axios數(shù)據(jù)請求get、post方法及實例詳解

    vue axios數(shù)據(jù)請求get、post方法及實例詳解

    axios是一個基于Promise,同時支持瀏覽器端和Node.js的HTTP庫,常用于Ajax請求。這篇文章主要介紹了vue axios數(shù)據(jù)請求get、post方法的使用 ,需要的朋友可以參考下
    2018-09-09
  • 一篇文章讓你看懂封裝Axios

    一篇文章讓你看懂封裝Axios

    axios的封裝和api接口的統(tǒng)一管理,其實主要目的就是在幫助我們簡化代碼和利于后期的更新維護,這篇文章主要給大家介紹了關(guān)于封裝Axios的相關(guān)資料,需要的朋友可以參考下
    2022-01-01

最新評論

汾阳市| 谢通门县| 日土县| 兴文县| 河东区| 淳安县| 霍山县| 靖江市| 崇明县| 寻甸| 广西| 朝阳县| 沙雅县| 新兴县| 潜山县| 泊头市| 伊宁市| 天水市| 武鸣县| 大安市| 洪雅县| 沈阳市| 绵竹市| 仙居县| 怀柔区| 曲水县| 盐亭县| 二手房| 星子县| 马公市| 炎陵县| 德钦县| 迁西县| 松江区| 察隅县| 五台县| 沛县| 柳州市| 贵德县| 尤溪县| 沁水县|