Alpine Tech
Alpine Tech

Reputation: 1

Problems uploading jpg with OneDrive Rest Api

For this HTML5 phone App I have to use the OneDrive REST API.

I managed to upload the file, but the file up on the server is not in .jpg format and won't display in a jpg viewer. After logging in through the REST API and getting a token back, I used XMLHttpRequest to PUT the file. I've tried passing it up as an ArrayBuffer and as a base64 encoded string. In each case the file makes it there, but is not decoded properly. I figured I needed to tell the webserver how to decode the file with "request.setRequestHeader('Content-Type', 'image/jpg')" ,but I get back the following error message from the OneDrive server:

"error": { "code": "request_body_invalid_media_type", "message": "The Content-Type header 'text/plain' isn't supported." }

this.uploadFile = function (token, fileUri,fileName, fileTools, success, fail) {

    var me = app.log('||Microsoft.uploadFile||');

    fileTools.readFileContent(fileUri, 'dataUrl', gotData, fileError);

    function fileError(err) {
        app.logError(me + '::fileError:::' + err);
    }

    function gotData(base64String) {

        var request = new XMLHttpRequest;

        app.log(me + '::token::' + token);

        request.open("PUT", "https://apis.live.net/v5.0/me/skydrive/files/" + fileName + "?access_token=" + token, true);           
        request.setRequestHeader('Content-Type', 'text/plain');
        //request.setRequestHeader('Content-Type', 'image/jpg');
        //request.setRequestHeader('Content-Transfer-Encoding', 'base64;charset=utf-8');

        request.onload = function (e) {

            app.log(request.responseText);

            success(e);
        };

        request.onerror = function (e) {

            app.log(me + '::7::');

            fail(e.error.message);
        };

        app.log(me + '::8::' + base64String.substr(0,100));

        var b = new Base64Stuff();          

        var aBuffer = b.base64ToBuffer(b.removeBase64jpgHeader(base64String));

        request.send(aBuffer);

        //request.send(base64String);
    }
}

Upvotes: 0

Views: 995

Answers (1)

Jensan
Jensan

Reputation: 57

try

request.setRequestHeader('Content-Type', '');

Upvotes: 1

Related Questions