Uploading Files and Folders to Box
Hi Folks,
I am trying to upload a file using the box API - https://upload.box.com/api/2.0/files/content, however every time I am receiving the below mentioned error:
“XMLHttpRequest cannot load https://upload.box.com/api/2.0/files/content. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 400.”
Could you let me know what I am missing anything or if CORS needs to enabled then what should be the value of CORS as it’s a simple HTML file placed on my desktop.
Here is my code:
var form = new FormData();
// The content of the file
var fileBody = 'This is a test file';
// JS file-like object
var blob = new Blob([fileBody], { type: 'text/xml' });
// Add the file to the form
form.append('file', blob);
form.append('name', 'testfile');
// Add the destination folder for the upload to the form
form.append('parent_id', '0');
var uploadUrl = 'https://upload.box.com/api/2.0/files/content';
// The Box OAuth 2 Header. Add your access token.
var headers = {
Authorization: 'Bearer mydevToken'
};
$.ajax({
url: uploadUrl,
headers: headers,
type: 'POST',
async: false,
crossDomain: true,
crossOrigin: true,
// This prevents JQuery from trying to append the form as a querystring
processData: false,
contentType: false,
data: form,
success: function (data) {
// Log the JSON response to prove this worked
console.log(data.responseText);
},
error: function (error) {
alert(error);
}
});
Thanks in Advance!
Post is closed for comments.
Comments
4 comments