vue使用el-upload上傳文件及Feign服務(wù)間傳遞文件的方法
一、前端代碼
<el-upload class="step_content" drag action="string" ref="upload" :multiple="false" :http-request="createAppVersion" :data="appVersion" :auto-upload="false" :limit="1" :on-change="onFileUploadChange" :on-remove="onFileRemove"> <i class="el-icon-upload"></i> <div class="el-upload__text">將文件拖到此處,或<em>點(diǎn)擊上傳</em></div> </el-upload> <div class="mgt30"> <el-button v-show="createAppVisible" :disabled="createAppDisable" type="primary" class="mgt30" @click="onSubmitClick">立即創(chuàng)建 </el-button> </div> .... onSubmitClick() { this.$refs.upload.submit(); }, createAppVersion(param) { this.globalLoading = true; const formData = new FormData(); formData.append('file', param.file); formData.append('appVersion', JSON.stringify(this.appVersion)); addAppVersionApi(formData).then(res => { this.globalLoading = false; this.$message({message: '創(chuàng)建APP VERION 成功', type: 'success'}); this.uploadFinish(); }).catch(reason => { this.globalLoading = false; this.showDialog(reason); }) },
說(shuō)明:
- el-upload 的 ref="upload" 給這個(gè)組件起個(gè)變量名,以便 js邏輯代碼可以引用
- http-request="createAppVersion" el-upload 上傳使使用自定義的方法
- :data="appVersion" 上傳時(shí)提交的表單數(shù)據(jù)
- onSubmitClick 方法中會(huì)調(diào)用el-upload.submit()方法,進(jìn)而調(diào)用createAppVersion()方法
- 組成表單參數(shù),取得上傳的file 和 其它參數(shù)
const formData = new FormData(); formData.append('file', param.file); formData.append('appVersion', JSON.stringify(this.appVersion));
6.addAppVersionApi 最終會(huì)調(diào)用下面的方法,其中formData就是param, 請(qǐng)求需要加header: 'Content-Type': 'multipart/form-data'
function httpPostMutipartFileAsyn(url, param) { return new Promise((resolve, reject) => { request({ url: url, headers: { 'Content-Type': 'multipart/form-data' }, method: 'post', data: param }).then(response => { if (response.data.status.code === 0) { resolve(response.data) } else { reject(response.data.status) } }).catch(response => { reject(response) }) }) }
二、后端代碼
1.后端controller接口
@PostMapping("/version/add") public RestResult addAppVersion(@RequestParam("appVersion") String appVersion, @RequestParam("file") MultipartFile multipartFile) { .... return new RestResult(); }
三、Feign 實(shí)現(xiàn)服務(wù)間傳遞MultipartFile參數(shù)
Controller的addAppVersion()接口,收到上傳的文件后,需要通過(guò)Http調(diào)用遠(yuǎn)程接口,將文件上傳到資源服務(wù)。一開(kāi)始嘗試使用OKHttp 和 RestTemplate 實(shí)現(xiàn),但是這兩種方法都必須將文件先保存,無(wú)法直接傳遞MultipartFile參數(shù),然后才能通過(guò)OKHttp 和 RestTemplate提供的相關(guān)接口去實(shí)現(xiàn)。 本著不想在本地保存臨時(shí)文件的,找到了通過(guò)Feign解決的一種方式
1.添加如下依賴:
<dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form-spring</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.3</version> </dependency>
2.Feign 接口實(shí)現(xiàn)
@FeignClient(name = "resource-client",url = "http://xxxx",path = "resource",configuration = ResourceServiceFeignClient.MultipartSupportConfig.class) public interface ResourceServiceFeignClient { @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) Resource upload(@RequestPart("file") MultipartFile file); class MultipartSupportConfig { @Bean public Encoder feignFormEncoder() { return new SpringFormEncoder(); } } }
這里Feign是通過(guò)url實(shí)現(xiàn)的接口調(diào)用,并沒(méi)有通過(guò)SpringCloud注冊(cè)中心服務(wù)發(fā)現(xiàn)來(lái)實(shí)現(xiàn)接口調(diào)用,因?yàn)槲宜诘捻?xiàng)目是獨(dú)立在服務(wù)化體系外的
3.將Feign接口自動(dòng)注入,正常使用就行了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue實(shí)現(xiàn)數(shù)據(jù)控制視圖的原理解析
這篇文章主要介紹了vue如何實(shí)現(xiàn)的數(shù)據(jù)控制視圖的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01vue頁(yè)面離開(kāi)后執(zhí)行函數(shù)的實(shí)例
下面小編就為大家分享一篇vue頁(yè)面離開(kāi)后執(zhí)行函數(shù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03vue中對(duì)象數(shù)組去重的實(shí)現(xiàn)
這篇文章主要介紹了vue中對(duì)象數(shù)組去重的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02vue3使用element-plus中el-table組件報(bào)錯(cuò)關(guān)鍵字'emitsOptions'與&
這篇文章主要給大家介紹了關(guān)于vue3使用element-plus中el-table組件報(bào)錯(cuò)關(guān)鍵字'emitsOptions'與'insertBefore'的相關(guān)資料,文中將解決方法介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10在vue+element ui框架里實(shí)現(xiàn)lodash的debounce防抖
今天小編就為大家分享一篇在vue+element ui框架里實(shí)現(xiàn)lodash的debounce防抖,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11基于axios封裝fetch方法及調(diào)用實(shí)例
下面小編就為大家分享一篇基于axios封裝fetch方法及調(diào)用實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02