vue使用el-upload上傳文件及Feign服務間傳遞文件的方法
阿新 • • 發佈:2019-04-11
strong https 就是 url mes col quest post The
一、前端代碼
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<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>點擊上傳</em></div> </el-upload>
<div class= "mgt30" >
<el-button v-show= "createAppVisible" :disabled= "createAppDisable" type= "primary" class= "mgt30"
@click= "onSubmitClick" >立即創建
</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: ‘創建APP VERION 成功‘ , type: ‘success‘ });
this .uploadFinish();
}). catch (reason => {
this .globalLoading = false ;
this .showDialog(reason);
})
},
|
說明:
- el-upload 的 ref="upload" 給這個組件起個變量名,以便 js邏輯代碼可以引用
- http-request="createAppVersion" el-upload 上傳使使用自定義的方法
- :data="appVersion" 上傳時提交的表單數據
- onSubmitClick 方法中會調用el-upload.submit()方法,進而調用createAppVersion()方法
- 組成表單參數,取得上傳的file 和 其它參數
1 2 3 |
const formData = new FormData();
formData.append( ‘file‘ , param.file);
formData.append( ‘appVersion‘ , JSON.stringify( this .appVersion));
|
6.addAppVersionApi 最終會調用下面的方法,其中formData就是param, 請求需要加header: ‘Content-Type‘: ‘multipart/form-data‘
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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接口
?1 2 3 4 5 6 7 8 |
@PostMapping ( "/version/add" )
public RestResult addAppVersion( @RequestParam ( "appVersion" ) String appVersion,
@RequestParam ( "file" ) MultipartFile multipartFile) {
....
return new RestResult();
}
|
三、Feign 實現服務間傳遞MultipartFile參數
Controller的addAppVersion()接口,收到上傳的文件後,需要通過Http調用遠程接口,將文件上傳到資源服務。一開始嘗試使用OKHttp 和 RestTemplate 實現,但是這兩種方法都必須將文件先保存,無法直接傳遞MultipartFile參數,然後才能通過OKHttp 和 RestTemplate提供的相關接口去實現。 本著不想在本地保存臨時文件的,找到了通過Feign解決的一種方式
1.添加如下依賴:
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
< 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 接口實現
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
@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是通過url實現的接口調用,並沒有通過SpringCloud註冊中心服務發現來實現接口調用,因為我所在的項目是獨立在服務化體系外的
3.將Feign接口自動註入,正常使用就行了。
vue使用el-upload上傳文件及Feign服務間傳遞文件的方法