1

I have some problems uploading files to the "OneDrive Api".

I get the URL to upload de file:

 $.ajax({
                    type: "POST",
                    url: folderUrl,
                    headers: {
                        'Authorization': 'Bearer ' + access_token,
                        'Content-Type': 'application/json',
                    },
                    success: {.....}
});

And with it i get the "upload url". THen i read and upload the file that the user select (I need fragment the file):

var fileInput = $("#file");
var fileList = fileInput[0].files;
var file = fileList[0];
content_length = file.size;
inicio = 0;
var blob = file.slice(inicio, content_length);

fileReader.onloadend = function (evt) {
    if (evt.target.readyState == FileReader.DONE) { // DONE == 2
        var result = evt.target.result;

        $.ajax({
            method: "PUT",
            url: uploadUrl,
            headers: {
                'Authorization': 'Bearer ' + access_token,
                'Content-Range': "bytes " + inicio + "-" + ((inicio + evt.target.result.length)-1) + "/" + content_length,
            },
            data: result,
            success: function (result) {
                console.log("subió");
            },
            error: function (error) {
                console.log("falló");
                console.log(JSON.stringify(error, null));
            }
        })
        .done(function (msg) {
            alert("Data Saved: " + msg);
        });
    }
};

fileReader.readAsBinaryString(blob);

When i upload text files with content like

"Lorem ipsum dolor sit amet" (without special characters 'ñ', 'á', etc...)

It work.

But, when upload files with especial characters ("ñ", "á", "¿"....) show the error:

"code":"invalidRequest","message":"**Declared fragment length does not match the provided number of bytes**"

With files like ".doc", ".zip" and more too.

[EDIT]: I noticed the "á" change for "á" (For example).

I need help :/

Thanks

Emmanuel S.
  • 23
  • 1
  • 7

1 Answers1

0

It sounds like an encoding issue, and my guess is the culprit is readAsBinaryString. Try switching to readAsArrayBuffer and see if that helps.

This answer goes into a lot more details.

Community
  • 1
  • 1
Brad
  • 4,089
  • 2
  • 16
  • 26