vue通过临时url上传文件到oss | 跨域 | 请求头 | 等问题解决

axios设置

不知原因,看了一天的oss2-python的源码,用工具模拟各种请求,fiddler抓请求,最终发现,请求头中不能有Content-Type,且要携带Access-Control-Request-Headers,值设置为Content-Type,成功上传。

PS: 有大神知道原因的,希望给个解答,不胜感激

const ossService = axios.create({
	// ...
    headers:{
        'Access-Control-Request-Headers':'Content-Type',
        'Content-Type':''
    }
    // ...
})

点击拍照并上传

<template>
  <div>
    <input ref="input" @change="finishedTakePhoto"
           style="display: none" type="file"
           capture="'user'" accept="image/*" />
           
    <el-button @click="takePhoto"
               class="el-icon-plus verify-btn" 
               type="primary" circle />
  </div>
</template>
// ...
// 此处省略十万行
takePhoto(){
      this.$refs.input.click();
 },
finishedTakePhoto(){
      ossService.put( '上传的地址', this.$refs.input.files[0],
      ).then(res=>{
        console.log(res)
      }).catch(err=>{
        console.log(err)
      })
    }
// ...

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