天天看點

自定義element的el-upload檔案上傳請求

自定義element的el-upload檔案上傳請求

<template>
	<el-upload
	  action="/ws-truck/messenger/upload/image" 
	  :on-success="onSuccess"
	  :http-request="httpRequest"
	  name="file"
	  class="upload-demo"
	  with-credentials
	  drag
	></el-upload>
</template>

<script>
export default {
  methods:{
     httpRequest(options) {
	    return new Promise((resolve, reject) => {
	        const formData = new FormData()
	        formData.append("file", options.file)
	        $.ajax({
	            url: '/upload/image',
	            type: 'POST',
	            data: formData,
	            headers: {
	                'X-Requested-By': 'eshipping'
	            },
	            success: function (res) {
	              resolve(res)
	            },
	            error: function (err) {
	              reject(err)
	            }
	        })
	      })
	    }
	  },
	  onSuccess(response, file, fileList) {
	      if (response.success) {
		    const url = response.filePath
	        this.$emit('update:images', url)
	      }
	    },
   }
</script>