admin管理员组文章数量:1026620
Say I have a model Restaurant
, and I want to upload an image and link it to the model.
From documentation this should happen in two steps:
- Create new entity
- Upload and link image
Currently, after I create the entity and try to do step 2 it fails. Note: Image is obtained from React-Native image picker
Here is what I am doing:
const data = new FormData();
data.append('files', image.uri);
data.append('refId', id);
data.append('ref', 'Restaurants');
data.append('field', 'Logo');
What I see is that the image is not uploaded. Furthermore, debugging from Strapi side, I see the request with all these data as fields
.
I am using FormData
as mentioned in the documentation, what am I missing?
Say I have a model Restaurant
, and I want to upload an image and link it to the model.
From documentation this should happen in two steps:
- Create new entity
- Upload and link image
Currently, after I create the entity and try to do step 2 it fails. Note: Image is obtained from React-Native image picker
Here is what I am doing:
const data = new FormData();
data.append('files', image.uri);
data.append('refId', id);
data.append('ref', 'Restaurants');
data.append('field', 'Logo');
What I see is that the image is not uploaded. Furthermore, debugging from Strapi side, I see the request with all these data as fields
.
I am using FormData
as mentioned in the documentation, what am I missing?
1 Answer
Reset to default 4Turned out that I need to add some extra information to the files
key so that FormData
recongnize it as a file and Strapi can handle file upload.
Here is what works:
const data = new FormData();
data.append('files', {
uri: logo.uri,
name: `test.jpg`,
type: 'multipart/form-data'
});
data.append('refId', id);
data.append('ref', 'Restaurants');
data.append('field', 'Logo');
What matters really is the type: 'multipart/form-data
.
One more remark, in the documentation, there is another key called source
. I didn't use it and it seems not to affect anything. Note sure if it needed.
Say I have a model Restaurant
, and I want to upload an image and link it to the model.
From documentation this should happen in two steps:
- Create new entity
- Upload and link image
Currently, after I create the entity and try to do step 2 it fails. Note: Image is obtained from React-Native image picker
Here is what I am doing:
const data = new FormData();
data.append('files', image.uri);
data.append('refId', id);
data.append('ref', 'Restaurants');
data.append('field', 'Logo');
What I see is that the image is not uploaded. Furthermore, debugging from Strapi side, I see the request with all these data as fields
.
I am using FormData
as mentioned in the documentation, what am I missing?
Say I have a model Restaurant
, and I want to upload an image and link it to the model.
From documentation this should happen in two steps:
- Create new entity
- Upload and link image
Currently, after I create the entity and try to do step 2 it fails. Note: Image is obtained from React-Native image picker
Here is what I am doing:
const data = new FormData();
data.append('files', image.uri);
data.append('refId', id);
data.append('ref', 'Restaurants');
data.append('field', 'Logo');
What I see is that the image is not uploaded. Furthermore, debugging from Strapi side, I see the request with all these data as fields
.
I am using FormData
as mentioned in the documentation, what am I missing?
1 Answer
Reset to default 4Turned out that I need to add some extra information to the files
key so that FormData
recongnize it as a file and Strapi can handle file upload.
Here is what works:
const data = new FormData();
data.append('files', {
uri: logo.uri,
name: `test.jpg`,
type: 'multipart/form-data'
});
data.append('refId', id);
data.append('ref', 'Restaurants');
data.append('field', 'Logo');
What matters really is the type: 'multipart/form-data
.
One more remark, in the documentation, there is another key called source
. I didn't use it and it seems not to affect anything. Note sure if it needed.
本文标签: javascriptStrapi How to upload image and link it to ModelStack Overflow
版权声明:本文标题:javascript - Strapi: How to upload image and link it to Model? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745651567a2161350.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论