将图像上传到 strapi

新手上路,请多包涵

我想用 html 文件将图像上传到 strapi。当我运行代码时,出现错误:POST http://localhost:1337/upload 500(内部服务器错误)。

 $.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);
    }
});

原文由 calzone 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 482
1 个回答

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

 mimeType: "multipart/form-data"

您可以 在此处 查看文档

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

  2. 允许的参数是:

    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

将文件链接到条目

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

{
 "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.
}

原文由 xargr 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题