国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Vue+element Upload利用http-request實(shí)現(xiàn)第三方地址圖片上傳

這篇具有很好參考價(jià)值的文章主要介紹了Vue+element Upload利用http-request實(shí)現(xiàn)第三方地址圖片上傳。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

vue第三方地址圖片上傳+后端圖片上傳接口開發(fā)+postman測試圖片上傳

  1. Vue + element (el-upload)中的:http-request圖片上傳
  2. java后端上傳接口,利用OSS存儲(chǔ)圖片
  3. postman測試圖片上傳功能及方法

對比:服務(wù)端簽名后直傳


前言

使用element UI 的el-upload實(shí)現(xiàn)第三方地址圖片上傳,替換原來的action方法


一、Vue + el-upload

直接上傳方法如下:上傳圖片根據(jù)action地址請求后獲取到圖片url地址

Vue+element Upload利用http-request實(shí)現(xiàn)第三方地址圖片上傳
具體方法官網(wǎng)有定義,action為必傳項(xiàng)
Vue+element Upload利用http-request實(shí)現(xiàn)第三方地址圖片上傳
Vue+element Upload利用http-request實(shí)現(xiàn)第三方地址圖片上傳

二、實(shí)現(xiàn)第三方地址圖片上傳

不使用action地址,又由于action為必傳項(xiàng),改為 action=" #",新增 :http-request

頁面:

<el-form :model="form">
  <el-form-item label="圖片" v-model="form.imageUrl">
    <el-upload
      action="#"
      list-type="picture-card"
      :http-request="httpRequest"
      :before-upload="beforeAvatarUpload"
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt />
    </el-dialog>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="onSubmit">立即創(chuàng)建</el-button>
    <el-button>取消</el-button>
  </el-form-item>
</el-form>

方法:

methods: {
    httpRequest(data) {
      console.log("自定義上傳", data);
      // 封裝FormData對象
      var formData = new FormData();
      formData.append("file", data.file);
      console.log("formData",formData);
      // 調(diào)用后端接口
      uploadByServer(formData).then(res => {
        console.log(res);
      }).catch(err=>{})
    },
    beforeAvatarUpload(file) {
      //   console.log("上傳前", file);
      const isImg = file.size / 1024 / 1024 < 2;
      if (!isImg) {
        this.$message.error("上傳頭像圖片大小不能超過 2MB!");
      }

      const isType = file.type === "image/png";
      const isType2 = file.type === "image/jpeg";

      if (!isType && !isType2) {
        this.$message.error("上傳頭像圖片格式為png或jpg");
      }
      return (isType || isType2) && isImg;
    },
  }

結(jié)果:

Vue+element Upload利用http-request實(shí)現(xiàn)第三方地址圖片上傳

前端代碼:

<template>
  <div>
    <el-form :model="form">
      <el-form-item label="圖片" v-model="form.imageUrl">
        <el-upload
          action="#"
          list-type="picture-card"
          :http-request="httpRequest"
          :before-upload="beforeAvatarUpload"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt />
        </el-dialog>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即創(chuàng)建</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {uploadByServer} from "@/api/upload";
export default {
  data() {
    return {
      form: {
        imageUrl: ""
      },
      dialogImageUrl: "",
      dialogVisible: false
    };
  },
  methods: {
    httpRequest(data) {
      console.log("自定義上傳", data);
      var formData = new FormData();
      formData.append("file", data.file);
      console.log("formData",formData);
      uploadByServer(formData).then(res => {
        console.log(res);
      }).catch(err=>{})
    },
    beforeAvatarUpload(file) {
      //   console.log("上傳前", file);
      const isImg = file.size / 1024 / 1024 < 2;
      if (!isImg) {
        this.$message.error("上傳頭像圖片大小不能超過 2MB!");
      }

      const isType = file.type === "image/png";
      const isType2 = file.type === "image/jpeg";

      if (!isType && !isType2) {
        this.$message.error("上傳頭像圖片格式為png或jpg");
      }
      return (isType || isType2) && isImg;
    },
    handleChange(file, fileList) {
      if (fileList.length > 1) {
        fileList.shift();
      }
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    onSubmit() {
      console.log("submit!");
    }
  },
  created() {},
  mounted() {}
};
</script>
  }

二、后端接口

利用OSS實(shí)現(xiàn)簡單的圖片上傳功能:

引入oss依賴

  <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alicloud-oss</artifactId>
      <version>2.2.0.RELEASE</version>
  </dependency>

Controller層((@RequestParam MultipartFile file))

   @PostMapping("/uploadByServer")
   public R uploadByServer(@RequestParam MultipartFile file) {
       String s = bannerServiece.uploadByServer(file);
       return R.ok().put("data",s);
   }

接口實(shí)現(xiàn)

    public String uploadByServer(MultipartFile file) {
        // Endpoint以華東1(杭州)為例,其它Region請按實(shí)際情況填寫。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 阿里云賬號AccessKey擁有所有API的訪問權(quán)限,風(fēng)險(xiǎn)很高。強(qiáng)烈建議您創(chuàng)建并使用RAM用戶進(jìn)行API訪問或日常運(yùn)維,請登錄RAM控制臺(tái)創(chuàng)建RAM用戶。
        String accessKeyId = "yourAccessKeyId";
        String accessKeySecret = "yourAccessKeySecret";
        // 填寫B(tài)ucket名稱,例如examplebucket。
        String bucketName = "examplebucket";
        // 填寫Object完整路徑,完整路徑中不能包含Bucket名稱,例如exampledir/exampleobject.txt。
        String objectName = "banner/uat/";

        // 創(chuàng)建OSSClient實(shí)例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        String url = "";
        try {
            // 獲取上傳文件的輸入流
            InputStream inputStream = file.getInputStream();
            // 獲取文件原始名稱
            String filename = file.getOriginalFilename();

            // 創(chuàng)建PutObjectRequest對象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName + filename, inputStream);
            // 設(shè)置該屬性可以返回response。如果不設(shè)置,則返回的response為空。
            putObjectRequest.setProcess("true");
            // 調(diào)用oss方法實(shí)現(xiàn)上傳
            // 1、bucketName 2、上傳到oss文件路徑和文件名稱 3、文件的輸入流
//            PutObjectResult putObjectResult = ossClient.putObject(bucketName, objectName + filename, inputStream);
            PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);

            // 獲取url地址(根據(jù)阿里云oss中的圖片實(shí)例拼接字符串) 拼接url字符串
            // https://edu-leader.oss-cn-beijing.aliyuncs.com/%E4%BB%96.jpg
            String uri = putObjectResult.getResponse().getUri();
//            url = "https://" + bucketName + "." + "oss-cn-hangzhou.aliyuncs.com" + "/" + objectName + filename;
            url = uri;
            // 關(guān)閉oss
            ossClient.shutdown();
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return url;
    }

三、postman測試圖片上傳

Vue+element Upload利用http-request實(shí)現(xiàn)第三方地址圖片上傳文章來源地址http://www.zghlxwxcb.cn/news/detail-425607.html


總結(jié)

到了這里,關(guān)于Vue+element Upload利用http-request實(shí)現(xiàn)第三方地址圖片上傳的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • el-upload自定義上傳http-request

    el-upload自定義上傳http-request

    使用http-request自定義上傳,觸發(fā)on-success,on-error鉤子。 template js 也可以返回Promise,on-success,on-error鉤子就會(huì)被執(zhí)行。

    2024年02月07日
    瀏覽(22)
  • ElementUi 關(guān)于 el-upload的自定義上傳(http-request)的使用

    ElementUi 關(guān)于 el-upload的自定義上傳(http-request)的使用

    ?? 在開發(fā)中 遇到如下需求,要求前端傳參并導(dǎo)入excel表格。導(dǎo)入失敗,要彈出錯(cuò)誤信息,同時(shí)導(dǎo)出錯(cuò)誤信息的excel表格,導(dǎo)入成功提示信息即可。 參考官方文檔,把上傳需要的屬性加加入使用 ? 改造之前: 設(shè)置headers信息 在el-upload標(biāo)簽中加入http-request ,如下: 具體操作方法

    2024年02月01日
    瀏覽(23)
  • 06-HTTP-Request獲取請求頭數(shù)據(jù)方法

    1、getHeader()方法用于獲取指定名稱的HTTP請求頭的值。 getHeader()方法的參數(shù)為一個(gè)字符串,表示要獲取的HTTP請求頭的名稱。方法返回一個(gè)字符串,表示該HTTP請求頭的值。如果指定名稱的HTTP請求頭不存在,則返回null。 常見的HTTP請求頭有: Accept:指定客戶端可以接受哪些類型

    2024年02月16日
    瀏覽(16)
  • 07-HTTP-Request獲取請求體數(shù)據(jù)方法getReader()\getInputStream()

    只有POST請求方式,才有請求體,在請求體中封裝了POST請求的請求參數(shù)。 1、getReader()方法用于獲取HTTP請求體的字符流,可以用于讀取HTTP請求體的文本數(shù)據(jù)。getReader()方法返回BufferedReader對象,該對象提供了readLine()方法和read()方法,可以用于逐行或逐個(gè)字符地讀取HTTP請求體的

    2024年02月07日
    瀏覽(20)
  • Vue Element upload組件和Iview upload 組件上傳文件

    Vue Element upload組件和Iview upload 組件上傳文件

    今天要分享的是使用這倆個(gè)UI組件庫的upload組件分別實(shí)現(xiàn)調(diào)用組件本身的上傳方法實(shí)現(xiàn)和后臺(tái)交互。接下來就是開車的時(shí)間,請坐穩(wěn)扶好~ 一、element upload組件傳送門 ?1、html文件 注意事項(xiàng): 使用組件本身的上傳事件,必須加auto-upload屬性設(shè)置為false; ????????????????

    2024年02月11日
    瀏覽(27)
  • Vue - 使用Element UI Upload / importExcelJs進(jìn)行文件導(dǎo)入

    Vue - 使用Element UI Upload / importExcelJs進(jìn)行文件導(dǎo)入

    1 情景一 需求背景 : 后端配合 ,點(diǎn)擊\\\"導(dǎo)入\\\"按鈕,彈出“導(dǎo)入”彈窗,將電腦本地Excel表格數(shù)據(jù) 導(dǎo)入 到頁面中表格位置(需要調(diào)用后端接口),而頁面中表格通過后端接口獲取最新數(shù)據(jù)。 實(shí)現(xiàn)思路 :彈窗嵌入 Element UI Upload 上傳組件,獲取到文件后調(diào)后端接口。 action : 上傳

    2024年02月03日
    瀏覽(14)
  • vue Element ui上傳組件el-upload封裝

    注釋: 1. limit 可上傳圖片數(shù)量 2. lableName 當(dāng)前組件name,用于一個(gè)頁面多次使用上傳組件,對數(shù)據(jù)進(jìn)行區(qū)分 3. upload 上傳圖片發(fā)生變化觸發(fā),返回已上傳圖片的信息 4. imgUrl 默認(rèn)圖片

    2024年02月11日
    瀏覽(30)
  • vue3中element-plus Upload上傳文件

    vue3中element-plus Upload上傳文件

    先上效果圖: ?上傳后: ?頁面: 我這里做個(gè)限制,僅限上傳一個(gè)pdf文件,如果不需要可以去掉,更多api請參考官方upload上傳官方文檔 js部分: 完結(jié),撒花~

    2024年02月13日
    瀏覽(34)
  • Element UI Plus + Vue3 給 Upload設(shè)置請求頭

    問題描述 在vue項(xiàng)目中我們發(fā)送 ajax 請求一般都會(huì)使用 axios,并在 axios 中設(shè)置axios.defaults.baseURL,請求的基本地址,并在請求攔截器中設(shè)置 headers 使用 el-upload 上傳組件時(shí),action 為必選參數(shù),上傳的地址。 但此時(shí)我們?yōu)閍ction填寫地址不能不寫基本地址而僅寫 upload,要寫完整的

    2024年02月21日
    瀏覽(23)
  • 前端vue elementUI upload上傳組件封裝&多文件上傳&進(jìn)度條,后端servlet request.getPart()接收文件信息

    前端vue elementUI upload上傳組件封裝&多文件上傳&進(jìn)度條,后端servlet request.getPart()接收文件信息

    選中多個(gè)文件上傳 通過 axios請求 onUploadProgress 方法監(jiān)聽 on-progress on-success 用這兩個(gè)鉤子函數(shù)實(shí)現(xiàn)進(jìn)度條 下面有對應(yīng)的函數(shù)。 本文是每個(gè)文件一個(gè)請求上傳 也可以用一個(gè)請求上傳多個(gè)文件,需要將文件遍歷添加到 form 表單中,后端用 request.getParts(); 獲取集合,有需要的可以改

    2024年02月11日
    瀏覽(33)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包