none
Unable To Put/Upload content to a file in OneDrive For Business using OneDrive for Business API RRS feed

  • Question

  • I am trying to put/add content to file in OneDrive for Business using OneDrive for Business REST API.
    Below is the code snippet:
     
         var getFile = getFileBuffer(file);
          getFile.done(function (arrayBuffer) {
            var content = arrayBuffer;
            var query = "https://myonedrive/_api/v2.0/drive/{driveId}/items/{parentfolderId}/children/{fileName}/content";
          $.ajax({
              url: query,
              method: "PUT",
              data: arrayBuffer,  
              headers: {
              "accept": "application/json;odata=verbose",
              "content-length": arrayBuffer.byteLength
              },
              beforeSend: function (xhr) {
                    xhr.setRequestHeader("Authorization", "Bearer " + bearerTokenvalue);
                   },
         success: function (data) {
                  return
               },
         error: function (err) {
                    return;
                }
            })
          });
        function getFileBuffer(file) {
            var deferred = jQuery.Deferred();
            var reader = new FileReader();
            reader.onloadend = function (e) {
                deferred.resolve(e.target.result);
            }
            reader.onerror = function (e) {
                deferred.reject(e.target.error);
            }
            reader.readAsArrayBuffer(file);
            return deferred.promise();
        }

    I am getting below error in the success call and content is not getting uploaded in the file.

    error  message :"The HTTP header ACCEPT is missing or its value is invalid." 

    Thursday, February 25, 2016 10:48 AM

All replies

  • Hi SKukreja,

    Based on the error message, It think the accept parameter value in the header is incorrect.

    Based on the API document, it doesn’t say that we need to specify the accept parameter. Please remove it and try again.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, February 26, 2016 6:18 AM
    Moderator
  • Hello Starain,

    Removing accept header from the request , resolves the error in the success callback, however content is still not getting uploaded in the file. Moreover, the file present in OneDrive is getting corrupted.

    Thanks,

    SKukreja

    Friday, February 26, 2016 8:07 AM
  • I  am unable to upload file  with contents to onedrive for business using REST API. Below is the code:

    var getFile = getFileBuffer(file); getFile.done(function (arrayBuffer) { var content = arrayBuffer; var query = "https://myonedrive/personal/myonedrive/_api/web/GetFolderByServerRelativeUrl(@fileUrl)/Files/Add(url=@url)?@fileUrl='/personal/myonedrive/Documents/FolderName'&@url='FileName'"; $.ajax({ url: query, method: "POST", data: arrayBuffer, headers: { "accept": "application/json;odata=verbose", "content-length": arrayBuffer.byteLength }, beforeSend: function (xhr) { xhr.setRequestHeader("Authorization", "Bearer " + bearerTokenvalue); }, success: function (data) { return }, error: function (err) { return; } }) }); function getFileBuffer(file) { var deferred = jQuery.Deferred(); var reader = new FileReader(); reader.onloadend = function (e) { deferred.resolve(e.target.result); } reader.onerror = function (e) { deferred.reject(e.target.error); } reader.readAsArrayBuffer(file); return deferred.promise(); }

    With above code, a file is getting created with the specified name, however content is not getting uploaded to it .It just creates a file which is corrupted.

    Friday, February 26, 2016 10:17 AM
  • Hi SKukreja,

    Does it success callback?

    >> however content is still not getting uploaded in the file. Moreover, the file present in OneDrive is getting corrupted.

    Do you mean it uploads some data but getting corrupted?

    Based on the API, it has the content-type (text/plain), please use developer tool or fiddler tool to capture the request. You may specify content type in request.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 29, 2016 3:08 AM
    Moderator
  • Hi SKukreja,

    I think the content is incorrect, please specify the content type in request, also check the request through tool.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 29, 2016 7:49 AM
    Moderator
  • Hello Starain,

    Yes, it is a success callback, and the file is getting corrupted on onedrive. I have also tried with the content type as "text/plain", but still facing the same issue.

    Note: Here I want to upload an excel file to onedrive.

    Thanks,

    SKukreja

    Tuesday, March 1, 2016 11:24 AM
  • Hi SKukreja,

    Please use developer tool/fiddler tool to check the request and post the detail result here.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, March 2, 2016 3:15 AM
    Moderator