Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
uploadFile () {
// CSV File
const csv = this.get("csv");
// Data to pass to the server
let data;
// Option 1) Use FormData (preferred)
data = new FormData();
data.append("csv", csv, "csv");
// Option 2) Pass a raw object
data = {csv};
// Option 3) Pass file directly
data = csv;
// 1st Transport Method (preferred)
$.ajax({
type: "POST",
url: this.url(),
data,
success () {
console.log("success");
},
error () {
console.log("error");
},
xhrFields: {
onprogress (progress) {
console.log("progress", progress);
}
},
processData: false,
contentType: csv.type
});
// 2nd Transport Method, use backbone's built in
this.save(data, {
xhrFields: {
onprogress (progress) {
console.log("progress", progress);
}
},
processData: false,
contentType: csv.type
});
// 3rd Transport Method, XMLHttpRequest
const xhr = new XMLHttpRequest();
xhr.open("POST", this.url(), true);
xhr.onload = () => {
console.log("onload", xhr);
};
xhr.onerror = () => {
console.log("err", xhr);
};
xhr.upload.onprogress = (event) => {
console.log("progress", event, xhr);
};
xhr.setRequestHeader("Content-Type", csv.type);
xhr.setRequestHeader("X-System", "fub-spa");
xhr.send(data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.