From b42a49a6d86204d630a5efbefdc5821521403fcd Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 29 May 2018 05:30:23 +0200 Subject: upload files!! --- app/client/api/crud.upload.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'app/client/api') diff --git a/app/client/api/crud.upload.js b/app/client/api/crud.upload.js index f680a74..97b6769 100644 --- a/app/client/api/crud.upload.js +++ b/app/client/api/crud.upload.js @@ -1,12 +1,20 @@ import { as_type } from './crud.types' -export function crud_upload(type, id, fd, dispatch) { +export function crud_upload(type, fd, data, dispatch) { return new Promise( (resolve, reject) => { + const id = data.id + + Object.keys(data).forEach(key => { + if (key !== 'id') { + fd.append(key, data[key]) + } + }) + const xhr = new XMLHttpRequest() xhr.upload.addEventListener("progress", uploadProgress, false) xhr.addEventListener("load", uploadComplete, false) xhr.addEventListener("error", uploadFailed, false) - xhr.addEventListener("abort", uploadCanceled, false) + xhr.addEventListener("abort", uploadCancelled, false) xhr.open("POST", '/' + type + '/' + id + '/upload/') xhr.send(fd) @@ -47,7 +55,7 @@ export function crud_upload(type, id, fd, dispatch) { }) } - uploadFailed = function (evt) { + function uploadFailed (evt) { dispatch && dispatch({ type: as_type(type, 'upload_error'), error: 'upload failed', @@ -55,7 +63,7 @@ export function crud_upload(type, id, fd, dispatch) { }) } - uploadCancelled = function (evt) { + function uploadCancelled (evt) { dispatch && dispatch({ type: as_type(type, 'upload_error'), error: 'upload cancelled', -- cgit v1.2.3-70-g09d2