Commit e86fb9a3 authored by Alf Eaton's avatar Alf Eaton

Add a file upload method that uses XMLHttpRequest

parent 91af56a0
Pipeline #1873 passed with stage
in 3 minutes and 41 seconds
import request from '../helpers/api'
import endpoint from '../helpers/endpoint'
import token from '../helpers/token'
import * as T from './types'
function fileUploadRequest () {
......@@ -49,3 +51,35 @@ export function fileUpload (file) {
)
}
}
export function fileUploadXHR (file) {
return (dispatch) => {
dispatch(fileUploadRequest())
const data = new FormData()
data.append('file', file)
const request = new XMLHttpRequest()
request.open('POST', endpoint + '/upload')
request.setRequestHeader('Authorization', 'Bearer ' + token())
request.setRequestHeader('Accept', 'text/plain') // the response is a URL
request.send(data)
request.addEventListener('load', event => {
if (request.status === 200) {
// response success
dispatch(fileUploadSuccess(file))
} else {
// response error
dispatch(fileUploadFailure('There was an error uploading the file'))
}
})
// network error
request.addEventListener('error', event => {
dispatch(fileUploadFailure('There was an error uploading the file'))
})
return request
}
}
import fetch from 'isomorphic-fetch'
const API_ENDPOINT = CONFIG['pubsweet-server'].API_ENDPOINT
import endpoint from './endpoint'
// read the authentication token from LocalStorage
import getToken from './token'
......@@ -24,7 +23,7 @@ const request = (url, options = {}) => {
}
if (!url.match(/^https?:/)) {
url = API_ENDPOINT + url
url = endpoint() + url
}
return fetch(url, options).then(response => {
......
/* global CONFIG */
module.exports = () => CONFIG['pubsweet-server'].API_ENDPOINT
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment