Skip to content
This repository has been archived by the owner on Jan 29, 2019. It is now read-only.

Commit

Permalink
fix: handle response parsing when responseType given
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsasharegan committed Nov 28, 2017
1 parent 9b51783 commit c66300d
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/components/VueTransmit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ import { Component, Prop, Watch } from "vue-property-decorator"
import noop from "lodash-es/noop"
import identity from "lodash-es/identity"
import {
READY_STATES,
hbsRegex,
hbsReplacer,
objFactory,
Expand Down Expand Up @@ -567,7 +566,6 @@ export default class VueTransmit extends Vue {
this.uploadFiles([file])
}
uploadFiles(files: VTransmitFile[]): void {
let response = null
const xhr = new XMLHttpRequest()
xhr.timeout = this.timeout
for (const file of files) {
Expand All @@ -584,22 +582,24 @@ export default class VueTransmit extends Vue {
xhr.upload.addEventListener("progress", updateProgress)
xhr.addEventListener("timeout", this.handleTimeout(files, xhr))
xhr.addEventListener("load", e => {
if (files[0].status === STATUSES.CANCELED || xhr.readyState !== READY_STATES.DONE) {
if (files[0].status === STATUSES.CANCELED || xhr.readyState !== XMLHttpRequest.DONE) {
return
}
response = xhr.responseText
if (xhr.responseType !== "arraybuffer" && xhr.responseType !== "blob") {
if (
xhr.getResponseHeader("content-type") &&
~xhr.getResponseHeader("content-type").indexOf("application/json")
) {
let response = xhr.response
if (!xhr.responseType) {
let contentType = xhr.getResponseHeader("content-type")
response = xhr.responseText
if (contentType && contentType.indexOf("application/json") > -1) {
try {
response = JSON.parse(response)
} catch (err) {
response = "Invalid JSON response from server."
}
}
}
// Called at load (when complete) will enable all the progress done logic.
updateProgress()
if (xhr.status < 200 || xhr.status >= 300) {
Expand Down

0 comments on commit c66300d

Please sign in to comment.