将图片上传到 Strapi [英] Upload image to strapi

查看:50
本文介绍了将图片上传到 Strapi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将带有 html 文件的图像上传到 Strapi.当我运行代码时,出现错误:POST http://localhost:1337/upload 500 (Internal Server错误).

I would like to upload an image to strapi with a html file. When I run the code, I obtain the error: POST http://localhost:1337/upload 500 (Internal Server Error).

$.ajax({
    type: 'POST',
    url: 'http://localhost:1337/upload',
    datatype: 'image/jpeg',
    data: JSON.stringify(img),
    complete: function(product) {
        console.log('Congrats, your product has been successfully created: ', product.description);
    },
    fail: function(error) {
        console.log('An error occurred:', error);
    }
});

推荐答案

正如我所见忘记添加 multipart/form-data

As I can see forgetting to add multipart/form-data

mimeType: "multipart/form-data"

您可以在此处查看文档

  1. 确保使用 multipart/form-data 编码发送请求

  1. Ensure that have send the request using multipart/form-data encoding

允许的参数有:

files: The file(s) to upload. The value(s) can be a Buffer or Stream.

path: (optional): The folder where the file(s) will be uploaded to (only supported on strapi-upload-aws-s3 now).

refId: (optional): The ID of the entry which the file(s) will be linked to.

ref: (optional): The name of the model which the file(s) will be linked to.

source: (optional): The name of the plugin where the model is located.

field: (optional): The field of the entry which the file(s) will be precisely linked to.

单个文件请求

curl -X POST -F 'files=@/path/to/pictures/file.jpg' http://localhost:1337/upload

将文件链接到条目

例如,您在名为 avatar 的用户模型中有链接图像字段

For example that you have link image field in User model named avatar

{
 "files": "...", // Buffer or stream of file(s)
 
 "path": "user/avatar", // Uploading folder of file(s).
 
 "refId": "5a993616b8e66660e8baf45c", // User's Id.
 
 "ref": "user", // Model name.
 
 "source": "users-permissions", // Plugin name.
 
 "field": "avatar" // Field name in the User model.
}

这篇关于将图片上传到 Strapi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆