0

我有一种必须通过 Curl 的 http.post 类型,请输入以下示例:

curl -X POST -F 'files=@/path/to/pictures/avatar.jpg&refId=5a993616b8e66660e8baf45c&ref=user&source=users-permissions&field=avatar' http://localhost:1337/upload

如您所见,将图像发送到特定字段。此卷曲由此 JSON 表示:

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

喜欢这个文档:https ://strapi.io/documentation/3.xx/guides/upload.html#examples

好吧,在 Angular 中这样做:

在我的组件的 html 中创建一个输入:

<input type="file" name="file" (change)="onFileSelected($event)">

创建您的活动:

onFileSelected(event) {
    console.log(event);
}

我在其中的文件中有几个结果:

files: FileList
0: File(58456)
lastModified: 1542844204930
lastModifiedDate: Wed Nov 21 2018 21:50:04 GMT-0200 (Horário de Verão de Brasília) {}
name: "8.jpg"
size: 58456
type: "image/jpeg"
webkitRelativePath: ""

好的,但是使用 Postman 进行任何测试以更好地了解我可以执行此操作的人,返回“true”,但我的数据库中没有任何反应:

在此处输入图像描述

由于我已经拥有了我需要的所有数据,我如何使用开头显示的 curl 和 post-Angled 方法,甚至 Postman(这会有更清晰的想法)?

4

1 回答 1

0

Here is how to did it with jQuery, you will just have to adapte it with Angular.

You also will have to add your ref attributes to make the relation with your user.

<form method="post">
    <input type="file" name="files" id="files">
    <input type="submit" name="" value="Submit">
  </form>

  <script type="text/javascript">
    $('form').on('submit', function (e) {
      e.preventDefault();

      var data = new FormData();
      $.each($('#files')[0].files, function(i, file) {
        data.append('files', file);
      });

      $.ajax({
        url: '/upload',
        data: data,
        contentType: false,
        processData: false,
        method: 'POST',
        success: function(data){
          alert(data);
        }
      });
    });
  </script>
于 2018-12-10T14:12:45.357 回答