最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Upload image to strapi - Stack Overflow

programmeradmin4浏览0评论

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),
    plete: function(product) {
        console.log('Congrats, your product has been successfully created: ', product.description);
    },
    fail: function(error) {
        console.log('An error occurred:', error);
    }
});

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),
    plete: function(product) {
        console.log('Congrats, your product has been successfully created: ', product.description);
    },
    fail: function(error) {
        console.log('An error occurred:', error);
    }
});
Share Improve this question edited Aug 7, 2018 at 9:28 Tim Diekmann 8,47611 gold badges48 silver badges72 bronze badges asked Aug 7, 2018 at 8:51 calzonecalzone 511 gold badge1 silver badge3 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

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

mimeType: "multipart/form-data"

You can see documentation here

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

  2. The parameters allowed are:

    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.
    

Single file request

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

Linking files to an entry

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

As of today, you can upload a file to strapi using axios with the following code. This is the handler of the input event on a file input.

onInput (files) {
  const axios = require('axios')

  const STRAPI_BASE_URL = 'http://localhost:1337'

  const formData = new FormData()
  formData.append('files', files)
  formData.append('ref', 'restaurant') // optional, you need it if you want to link the image to an entry
  formData.append('refId', 12345) // optional, you need it if you want to link the image to an entry
  formData.append('field', 'image') // optional, you need it if you want to link the image to an entry

  axios.post(`${STRAPI_BASE_URL}/upload`, formData)
}

I assume that you have a collection called restaurant with a field image of type file.

More on this here: https://strapi.io/documentation/v3.x/plugins/upload.html#upload-files

Here is another way using RestRequest in Winforms with C#:

var client = new RestClient("http://my-strapi-server:1337/upload");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddFile("files", @"C:\temp\bryan.jpg");
request.AddParameter("ref", "Singer");
request.AddParameter("refId", "605fe0c8sakhasg4c40253a");
request.AddParameter("field", "logo");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
发布评论

评论列表(0)

  1. 暂无评论