Skip to content

Instantly share code, notes, and snippets.

@seanrose
Last active October 28, 2022 20:16
Show Gist options
  • Select an option

  • Save seanrose/5570650 to your computer and use it in GitHub Desktop.

Select an option

Save seanrose/5570650 to your computer and use it in GitHub Desktop.

Revisions

  1. seanrose revised this gist May 13, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions sampleBoxFileUpload.js
    Original file line number Diff line number Diff line change
    @@ -21,15 +21,15 @@ form.append('file', blob);
    // Add the destination folder for the upload to the form
    form.append('parent_id', '0');

    var upload_url = 'https://upload.box.com/api/2.0/files/content';
    var uploadUrl = 'https://upload.box.com/api/2.0/files/content';

    // The Box OAuth 2 Header. Add your access token.
    var headers = {
    Authorization: 'Bearer YOUR_ACCESS_TOKEN'
    };

    $.ajax({
    url: upload_url,
    url: uploadUrl,
    headers: headers,
    type: 'POST',
    // This prevents JQuery from trying to append the form as a querystring
  2. seanrose created this gist May 13, 2013.
    42 changes: 42 additions & 0 deletions sampleBoxFileUpload.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    // Requires JQuery and CORS enabled for the Origin you're testing from.
    // Uncomment the next 4 lines to import JQuery
    // var script= document.createElement('script');
    // script.type= 'text/javascript';
    // script.src= '//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js';
    // document.head.appendChild(script);

    // Set up the multipart form using HTML5 FormData object
    // https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/FormData
    var form = new FormData();

    // The content of the file
    var fileBody = '<p>hey!<p>';

    // JS file-like object
    var blob = new Blob([fileBody], { type: 'text/xml'});

    // Add the file to the form
    form.append('file', blob);

    // Add the destination folder for the upload to the form
    form.append('parent_id', '0');

    var upload_url = 'https://upload.box.com/api/2.0/files/content';

    // The Box OAuth 2 Header. Add your access token.
    var headers = {
    Authorization: 'Bearer YOUR_ACCESS_TOKEN'
    };

    $.ajax({
    url: upload_url,
    headers: headers,
    type: 'POST',
    // This prevents JQuery from trying to append the form as a querystring
    processData: false,
    contentType: false,
    data: form
    }).complete(function ( data ) {
    // Log the JSON response to prove this worked
    console.log(data.responseText);
    });