Reactjs 在React中将图像存储上传到google云存储
我想上传图片并将其存储到谷歌云存储中,代码如下。但是,我得到了这样的错误“在访问XMLHttpRequest时”https://storage.googleapis.com/uploadimage12“起源”http://localhost:3002'已被CORS策略阻止:请求的资源上不存在'Access Control Allow Origin'标头”和“POST net::ERR_FAILED”Reactjs 在React中将图像存储上传到google云存储,reactjs,image,upload,google-cloud-storage,bucket,Reactjs,Image,Upload,Google Cloud Storage,Bucket,我想上传图片并将其存储到谷歌云存储中,代码如下。但是,我得到了这样的错误“在访问XMLHttpRequest时”https://storage.googleapis.com/uploadimage12“起源”http://localhost:3002'已被CORS策略阻止:请求的资源上不存在'Access Control Allow Origin'标头”和“POST net::ERR_FAILED” import React,{Component}来自'React'; 从“axios”导入axi
import React,{Component}来自'React';
从“axios”导入axios
导出类建模扩展组件{
陈述={
selectedFile:空
}
fileChangedHandler=事件=>{
这是我的国家({
selectedFile:event.target.files[0]
})
}
fileUploadHandler=()=>{
const fd=new FormData();
fd.append('image',this.state.selectedFile,this.state.selectedFile.name);
轴心柱https://storage.googleapis.com/uploadimage12",fd)
.然后(功能(响应){
控制台日志(响应);
});
}
上传处理程序
render(){
返回(
三维建模
上载二维图像以获取三维模型
上传
{" "}
渲染三维模型
{""}
)
}
}
导出默认建模
谁能给我一些帮助吗?我真的很感激 您好@Sunny考虑到您看到的错误和您的代码,您似乎没有正确配置CORS设置。请您按照云存储CORS的相关说明看一看并尝试一下好吗?
import React, {Component} from 'react';
import axios from 'axios'
export class modeling extends Component {
state={
selectedFile: null
}
fileChangedHandler = event => {
this.setState({
selectedFile: event.target.files[0]
})
}
fileUploadHandler = () => {
const fd = new FormData();
fd.append ('image', this.state.selectedFile,this.state.selectedFile.name);
axios.post('https://storage.googleapis.com/uploadimage12', fd)
.then(function(response){
console.log(response);
});
}
uploadHandler
render(){
return (
<div id="modeling">
<div className="container">
<div className="row">
<div className="col-xs-12 col-md-8">
<div className="about-text">
<h2>3D MODELING</h2>
<h3>Upload a 2D image to get a 3D model</h3>
<input className="btn btn-secondary" type="file" onChange={this.fileChangedHandler} />
<a>
<button className="btn btn-primary" onClick={this.fileUploadHandler} id="renderButton">
Upload
</button>
</a>{" "}
<a >
<button className="btn btn-primary" ng-click="showConfirm() " ng-if="fileName.length > 0" id="renderButton">
Render 3D Model
</button>
</a>{""}
</div>
</div>
</div>
</div>
</div>
)
}
}
export default modeling