eladmin 如何实现文件的上传

 前端页面



 封装api

import requestMultipart from './httpsMultipart'

export function uploadFile(data) {
  return requestMultipart({
    url: '/api/upload',
    method: 'post',
    data
  })
}

 封装https

const requestMultipart = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: Config.timeout 

})

requestMultipart.interceptors.request.use(
  config => {
    if (getToken()) {
      config.headers['Authorization'] = getToken() 
    }
    config.headers['Content-Type'] = "multipart/form-data;charset=utf-8";
    return config
  },
  error => {
    Promise.reject(error)
  }
)

requestMultipart.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    if (error.response.data instanceof Blob && error.response.data.type.toLowerCase().indexOf('json') !== -1) {
      const reader = new FileReader()
      reader.readAsText(error.response.data, 'utf-8')
      reader.onload = function (e) {
        const errorMsg = JSON.parse(reader.result).message
        Notification.error({
          title: errorMsg,
          duration: 5000
        })
      }
    } else {
      let code = 0
      try {
        code = error.response.data.status
      } catch (e) {
        if (error.toString().indexOf('Error: timeout') !== -1) {
          Notification.error({
            title: '网络请求超时',
            duration: 5000
          })
          return Promise.reject(error)
        }
      }
      console.log(code)
      if (code) {
        if (code === 401) {
          store.dispatch('LogOut').then(() => {
            Cookies.set('point', 401)
            location.reload()
          })
        } else if (code === 403) {
          router.push({ path: '/401' })
        } else {
          const errorMsg = error.response.data.message
          if (errorMsg !== undefined) {
            Notification.error({
              title: errorMsg,
              duration: 5000
            })
          }
        }
      } else {
        Notification.error({
          title: '接口请求失败',
          duration: 5000
        })
      }
    }
    return Promise.reject(error)
  }
)
export default requestMultipart

后端方法 

@PostMapping(具体路径)
public String saveVue(String title,@RequestParam("file") MultipartFile file) throws IOException {
       	//拿到具体文件 file
            return "SUCCESS";
    }

你可能感兴趣的:(vue.js,javascript,前端,java)