Загрузить изображение в страпи

1

Я хотел бы загрузить изображение на ленту с html файлом. Когда я запускаю код, я получаю ошибку: 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);
    }
});
Теги:
strapi

1 ответ

0

Как я могу заметить, забывая добавить 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 Id.

 "ref": "user", // Model name.

 "source": "users-permissions", // Plugin name.

 "field": "avatar" // Field name in the User model.
}
  • 0
    Можете ли вы привести пример, используя axios или запрос? Я всегда получаю `{statusCode: 400, ошибка: 'Неверный запрос', сообщение: 'Файлы пусты'}`, когда я уже передаю файл в качестве буфера.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню