- Is this correct way to post data with file from Ember.js to Django-rest-framework in the code below. It does not look very nice in my opinion.
- Are there other solutions to POST file through ember-django-adapter?
model.js
export default DS.Model.extend({
task: DS.belongsTo('task/task'),
creator: DS.belongsTo('task/user'),
attachment: DS.attr(),
});
route.js
actions: {
saveFile() {
let model = this.controller.get('model');
let creator_id = this.get('session.data.authenticated.user.id');
let creator = this.store.peekRecord('task/user', creator_id);
model.set('creator', creator);
let fileUpload = document.getElementById('file-field').files[0];
let host = this.store.adapterFor('application').get('host');
let url = host + '/api/task/taskfiles/'
let formData = new FormData();
formData.append('task', model.get('task.id'));
formData.append('creator', creator_id);
formData.append('attachment', fileUpload);
let req = new XMLHttpRequest();
req.open('POST', url);
req.send(formData);
},
}
views.py
class TaskFileViewSet(viewsets.ModelViewSet):
queryset = TaskFile.objects.all()
serializer_class = TaskFileSerializer
parser_classes = (MultiPartParser, FormParser)
Aucun commentaire:
Enregistrer un commentaire