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

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

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

本文實(shí)例為大家分享了elementui+vue+axios實(shí)現(xiàn)文件上傳本地服務(wù)器的具體代碼,供大家參考,具體內(nè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的類型必須為 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ù)中某個(gè)文件夾下。 缺點(diǎn):文件的名稱
? ? ? ? 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ù)用法實(shí)例分析

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

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

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

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

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

    詳解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)

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

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

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

    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仿微信聊天界面效果實(shí)例(vue3組合式版本)

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

    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
  • 一篇文章讓你看懂封裝Axios

    一篇文章讓你看懂封裝Axios

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

最新評(píng)論

冀州市| 安化县| 安阳县| 綦江县| 赤城县| 汽车| 三明市| 新乡县| 泗阳县| 额尔古纳市| 福安市| 晋江市| 敦化市| 临西县| 易门县| 万安县| 景泰县| 同德县| 皋兰县| 清徐县| 通城县| 塔河县| 丹江口市| 鹤岗市| 天柱县| 红桥区| 九江市| 遵化市| 朝阳县| 顺昌县| 宣恩县| 县级市| 区。| 信丰县| 乳山市| 西畴县| 通州区| 浑源县| 任丘市| 岚皋县| 噶尔县|