两段相同的代码【async await】

一只大学生 / 2024-10-14 / 原文

async function doSubmitFile() {
  const fileInput = document.getElementById('fileInput')
  const fileObj = fileInput.files[0]

  const formData = new FormData()
  formData.append('file', fileObj)

  try{
    const response = await _axios.post("/api/employ/upload/", formData ,{headers:{'Content-Type': 'multipart/form-data'}})
    console.log(response.data)
  }catch(error){
    ElMessage.error(`文件上传失败,${error}`)
  }
}

function doSubmitFile2() {
  const fileInput = document.getElementById('fileInput')
  const fileObj = fileInput.files[0]

  const formData = new FormData()
  formData.append('file', fileObj)

  _axios.post("/api/employ/upload/", formData, {headers: {'Content-Type': 'multipart/form-data'}}).then((res) => {
    console.log(res.data)
  }).catch((error) => {
    ElMessage.error(`文件上传失败,${error}`)
  })

}