方法:使用ember.js上传文件

fab*_*ian 7 ember.js

我想知道如何使用ember.js进行真正的文件上传(将文件保存到服务器)

有什么好例子吗?

Ala*_*ong 5

看我另一个帖子的回答

<input
  multiple="true"
  onchange={{action "upload"}}
  accept="image/png,image/jpeg,application/pdf"
  type="file"
/>

actions: {
  upload: function(event) {
    const reader = new FileReader();
    const file = event.target.files[0];
    let imageData;

    // Note: reading file is async
    reader.onload = () => {
      imageData = reader.result;
      this.set(data.image', imageData);

      // additional logics as you wish
    };

    if (file) {
      reader.readAsDataURL(file);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

它只是有效。


brg*_*brg 2

如果您阅读下面链接中的答案,您将了解如何使用 emberjs 上传文件并保存到服务器:

使用 Ember 数据上传文件

在上面链接中“Toran Billups”提供的答案中,我从他的答案中复制了以下几行,将其保存到服务器:

var person = PersonApp.Person.createRecord({username: 'heyo', attachment: fileToUpload});

self.get('controller.target').get('store').commit()
Run Code Online (Sandbox Code Playgroud)