Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload a file to OneDrive using OneDrive REST API?

Please allow me to explain what I am doing and How I am doing.

What I am doing?

I am trying to upload a file to Onedrive using its REST API Source: one drive api documentation

I am using OneDrive fragments approch to do so as the file can be as huge as 5gb or as small as 1kb (Depends on the user)

Currently, I am doing it using POST-MAN Chrome extention to call APIes

How am I doing?

  1. Generated Access token
  2. Created Session and received Uploading url
  3. Uploading file using PUT request as given below

Selected file that I want to upload (File Size: 729676295 bytes) enter image description here

Added headers and sent the request and sent the request enter image description here Here is the result enter image description here

it is saying that the maximum fragment size is 67108864 bytes so I changed the value of

content-length : 67108864 and content-range : bytes 0-67108863/729676295

but then I get this error message: Declared fragment length does not match the provided number of bytes enter image description here

Please help me to figure out what should I pass in content-length and content-range.

Many Thanks for you attention.

like image 582
Vikas Bansal Avatar asked Nov 19 '15 11:11

Vikas Bansal


2 Answers

Finally, thanks to god after 2 days of struggle I found what was the problem.

There are some points that you need to keep in mind

  1. You have to divide a file to into equal parts and save it somewhere and upload each of them separately on the same URL that you get after creating a session.
  2. Content-Length must be the total numbers of bytes of the file that you are uploading in all upload requests of fragments.
  3. Content-Range will be like: 0-{fragmentLength-1}/{totalNumberOfBytesOfFile}(same as content-length) and from next fragment the Content-Range will be {uploadedBytes}-{uploadedBytes+nextSetOfBytes-1}/{totalNumberOfBytesOfFile}

Note: The Next set of bytes should be as long as the previous was. Do not worry because the last fragment can be different.

P.S: Do not use the content-range that you will receive after successfully uploading the fragment.

like image 149
Vikas Bansal Avatar answered Oct 10 '22 10:10

Vikas Bansal


enter image description here

{totalNumberOfBytesOfFile} is not same as content-length;

Content-Length is bytes of current fragment.

the image is an a example for upload a 4917 bytes file.

like image 34
zhiqin_guo Avatar answered Oct 10 '22 11:10

zhiqin_guo