0

I am following this and I want to upload any type of file to OneDrive. It says that it accepts a buffer for the file content but my following code does not seem to work in case of any type of file. The file gets uploaded but it cannot be opened so the contents are messed up for sure.

Using the following method I am trying to get the body contents so that I can send them with the request.

private fileToBuffer(file: File): Observable<any> {
    return Observable.create(observer => {
        var arrayBuffer;
        var fileReader = new FileReader();
        fileReader.onload = function () {
            arrayBuffer = this.result;
            observer.next(arrayBuffer);
            observer.complete();
        };
        fileReader.readAsArrayBuffer(file);
    });
}
2
  • 1
    Hi Ibrahim, try obtaining a network capture of the upload using a tool like Fiddler, and see if the uploaded content matches what you expect. It sounds like you may be uploading the file encoded in some way, and the server may be storing it raw without decoding it in the way that your uploading component expects. Commented May 5, 2016 at 18:27
  • @daspek A sample of how to use the upload API of OneDrive would be really helpful. I cannot locate any sample online and the documentation is not sufficient. Commented May 6, 2016 at 9:18

1 Answer 1

2

I did not notice that the Angular 2's http's PUT was taking the body as string. So, I resorted to using XHR to upload a file with its contents.

var oReq = new XMLHttpRequest();
oReq.open("PUT", url, true);
oReq.setRequestHeader("Content-Type", "text/plain");
oReq.onload = function(e){
    console.log('done');
};
oReq.send(arrayBuffer);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.