Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
pubsweet
pubsweet-client
Commits
e86fb9a3
Commit
e86fb9a3
authored
Aug 31, 2017
by
Alf Eaton
Browse files
Add a file upload method that uses XMLHttpRequest
parent
91af56a0
Pipeline
#1873
passed with stage
in 3 minutes and 41 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
src/actions/fileUpload.js
View file @
e86fb9a3
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
}
}
src/helpers/api.js
View file @
e86fb9a3
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
=>
{
...
...
src/helpers/endpoint.js
0 → 100644
View file @
e86fb9a3
/* global CONFIG */
module
.
exports
=
()
=>
CONFIG
[
'
pubsweet-server
'
].
API_ENDPOINT
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment