Reactjs 如何在react中使用graphene django和axios将图像上载到服务器?

Reactjs 如何在react中使用graphene django和axios将图像上载到服务器?,reactjs,graphql,axios,graphene-python,graphene-django,Reactjs,Graphql,Axios,Graphene Python,Graphene Django,是否有人可以与graphene django的模型共享突变代码,以创建图像上传的突变?我如何在react中使用Axios上传图像 我不想使用Apollo,因为我的整个项目都在使用graphene django和Axios。我面临的唯一问题是图像上传 这是我的密码: #models.py 类别产品(型号.型号): category=models.ForeignKey(category,on_delete=models.CASCADE, blank=True,null=True) status=mo

是否有人可以与graphene django的模型共享突变代码,以创建图像上传的突变?我如何在react中使用Axios上传图像

我不想使用Apollo,因为我的整个项目都在使用graphene django和Axios。我面临的唯一问题是图像上传

这是我的密码:

#models.py
类别产品(型号.型号):
category=models.ForeignKey(category,on_delete=models.CASCADE,
blank=True,null=True)
status=models.BooleanField(默认值=True)
product_name=models.CharField(最大长度=300,空白=True,空=True)
price=models.BigIntegerField(blank=True,null=True)
tax\u slab=models.IntegerField(
最大长度=100,默认值=0,选项=SLAB,null=True,blank=True)
description=models.TextField(最大长度=500,空白=True,空=True)
image=models.FileField(null=True,blank=True)
#Y.py
从graphene_django.forms.mutation导入DjangoModelFormMutation
类别ProductVaritation(DjangomodelFormVaritation):
类元:
form\u class=ProductForm
类别突变(graphene.AbstractType):
create_product=ProductMutation.Field()

请查看代码: 使用axios、graphene django和django graphql jwt上传图像

import React,{Component}来自'React';
从“axios”导入axios
从“./config.json”导入{graphql}
类ProfileUpload扩展了组件{
状态={
数据:{imageFile:}
}
handleImageChange=({currentTarget:input})=>{
const data={…this.state.data};
数据[input.name]=input.files[0];
console.log(input.name)
console.log(input.files[0])
this.setState({data});
}
handleImageUpload=async()=>{
const data=新表单数据();
data.append('imageFile',this.state.data.imageFile)
const query=`变异{
createProfileImage{
轮廓图像{
身份证件
使用者{
身份证件
电子邮件
}
形象
}              
}
}`
data.append('query',query)
for(变量对数据.entries()){
log(对[0]+','+对[1]);
}
让结果=”;
等待axios({
方法:“post”,
url:graphql,
数据:数据,
配置:{headers:{'Content-transfer-Encoding':'multipart/formdata','Content-Type':'application/graphql'},
事实上,
})
.然后(功能(响应){
控制台日志(响应);
结果=响应;
})
.catch(函数(响应){
console.log(响应)
结果=响应;
})
返回结果;
}
handleSubmit=异步(事件)=>{
event.preventDefault();
const uploadres=wait this.handleImageUpload()
console.log(uploadres.data,“uploadres”)
}
render(){
报税表(
this.handleSubmit(event)}>
轮廓图像
提交
);
}
}

导出默认配置文件上传请查看代码: 使用axios、graphene django和django graphql jwt上传图像

import React,{Component}来自'React';
从“axios”导入axios
从“./config.json”导入{graphql}
类ProfileUpload扩展了组件{
状态={
数据:{imageFile:}
}
handleImageChange=({currentTarget:input})=>{
const data={…this.state.data};
数据[input.name]=input.files[0];
console.log(input.name)
console.log(input.files[0])
this.setState({data});
}
handleImageUpload=async()=>{
const data=新表单数据();
data.append('imageFile',this.state.data.imageFile)
const query=`变异{
createProfileImage{
轮廓图像{
身份证件
使用者{
身份证件
电子邮件
}
形象
}              
}
}`
data.append('query',query)
for(变量对数据.entries()){
log(对[0]+','+对[1]);
}
让结果=”;
等待axios({
方法:“post”,
url:graphql,
数据:数据,
配置:{headers:{'Content-transfer-Encoding':'multipart/formdata','Content-Type':'application/graphql'},
事实上,
})
.然后(功能(响应){
控制台日志(响应);
结果=响应;
})
.catch(函数(响应){
console.log(响应)
结果=响应;
})
返回结果;
}
handleSubmit=异步(事件)=>{
event.preventDefault();
const uploadres=wait this.handleImageUpload()
console.log(uploadres.data,“uploadres”)
}
render(){
报税表(
this.handleSubmit(event)}>
轮廓图像
提交
);
}
}
导出默认配置文件上传