System Information
- Strapi Version:
- Operating System:
- Database:
- Node Version:
- NPM Version:
- Yarn Version:
When uploading an image using the /api/upload
endpoint with ref
, refId
, files
, and field
parameters, the image is successfully added to the media library but is not linked to the specified entry in the collection. As a result, the uploaded images do not appear when fetching the details of the entry.
My code is
Upload Api Endoint
STRAPI_UPLOAD_ENDPOINT: ‘api/upload’,
FORUM_POST_ENDPOINT: ‘api/forums’,
static async uploadImage(fileContent: Buffer, mailFile: any, forumId: number): Promise<any> {
try {
const fileBlob = new Blob([new Uint8Array(fileContent)], { type: mailFile.mimetype });
const formData = new FormData();
formData.append('files', fileBlob, mailFile.originalname);
formData.append('ref', 'api::forum.forum');
formData.append('refId', forumId);
formData.append('field', 'forum_image');
const response = await apiRequest(`${strapiconstants.STRAPI_UPLOAD_ENDPOINT}`, 'POST', formData);
return response;
} catch (error: any) {
throw new Error(`Image upload failed: ${error.message}`);
}
}
Create Forum
static async createForum(forumData: any, fileData: any): Promise<ApiResponse<Forum>> {
try {
const response: any = await apiRequest(`${strapiconstants.FORUM_POST_ENDPOINT}`, 'POST', { data: forumData });
const fileContentBase64 = fileData.buffer;
await this.uploadImage(fileContentBase64, fileData, response.data.data.id);
const forum: Forum = response.data as Forum;
return { success: true, data: forum };
} catch (err: any) {
return {
success: false,
error: err.response?.data?.error?.message || err.message,
};
}
}
Here the forum is being created , but when im fetching the forum details images are not being shown.
When i check the strapi portal , in the draft section image is linked to this forum , but in published section image is not linked