vue 使用 el-upload 上傳文件及Feign 服務間傳遞文件

1、前端代碼

<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);
        })

      },

說明:前端

  1. el-upload 的 ref="upload" 給這個組件起個變量名,以便 js邏輯代碼能夠引用
  2. http-request="createAppVersion" el-upload 上傳使使用自定義的方法
  3. :data="appVersion" 上傳時提交的表單數據
  4. onSubmitClick 方法中會調用el-upload.submit()方法,進而調用createAppVersion()方法
  5. 組成表單參數,取得上傳的file 和 其它參數
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'git

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)
    })
  })
}

2、後端代碼

1.後端controller接口github

@PostMapping("/version/add")
    public RestResult addAppVersion(@RequestParam("appVersion") String appVersion,
                                    @RequestParam("file") MultipartFile multipartFile) {

        ....
        
        return new RestResult();
    }

3、Feign 實現服務間傳遞MultipartFile參數

Controller的addAppVersion()接口,收到上傳的文件後,須要經過Http調用遠程接口,將文件上傳到資源服務。一開始嘗試使用OKHttp 和 RestTemplate 實現,可是這兩種方法都必須將文件先保存,沒法直接傳遞MultipartFile參數,而後才能經過OKHttp 和 RestTemplate提供的相關接口去實現。 本着不想在本地保存臨時文件的,找到了經過Feign解決的一種方式spring

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 接口實現app

@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註冊中心服務發現來實現接口調用,由於我所在的項目是獨立在服務化體系外的post

3.將Feign接口自動注入,正常使用就好了。this

相關文章
相關標籤/搜索