4

I'm following the Vimeo api's guide about resumable uploads (https://developer.vimeo.com/api/upload/videos#resumable-tus-upload). I get a response after the first request but I get an undefined response for the second request (step 2. Upload the video file). The guide tells to PATCH the binary data of the video file to the URL from upload.upload_link. I'm using readAsBinaryString() to convert the video file to binary data (I have tried also readAsArrayBuffer() but with the same result. What am I possibly doing wrong? This is the code:

var reader = new FileReader();

$.ajax({
  'url': 'https://api.vimeo.com/me/videos',
  'type': 'POST',
  'headers': {
    'Accept': 'application/vnd.vimeo.*+json;version=3.4',
    'Content-Type': 'application/json',
    'Authorization': 'bearer ' + accessToken
  },
  "data": JSON.stringify({ 
    "upload" : {
      "approach" : "tus",
      "size" : fileSize

    }
  }),
  'success': function (result) {
    $.ajax({
      'url': result.upload.upload_link,
      'type': 'PATCH',
      'headers': {
        'Tus-Resumable': '1.0.0',
        'Upload-Offset': 0,
        'Content-Type': 'application/offset+octet-stream'
      },
      'data': reader.readAsBinaryString(fileContent),
      'success': function (result) {
        console.log(result)
      }
    });
  }
});
Pontus
  • 81
  • 1
  • 4

2 Answers2

0

I was able to get this working in my ReactJs application. Looks like you are missing the reader.onload() method - https://developer.mozilla.org/en-US/docs/Web/API/FileReader/onload

I am saving the result of reader.readAsArrayBuffer to state.binaryData. I then pass binaryData into my axios/fetch request as data. It is working and i successfully uploaded video.

const reader = new FileReader();
const fileSize = e.target.files[0].size;
reader.onload = r => {
  this.setState({ binaryData: r.target.result, fileSize });
};
reader.readAsArrayBuffer(e.target.files[0]);
leonard0
  • 133
  • 3
  • 9
0

I have been working with same right now. I think when call Patch at that time it's not sure that your binary data of file is converted,

so to be sure you have to put that call into reader.onload. so it's been sure about binary data.

readAsBinaryString is deprecated. so you can use readAsArrayBuffer. Why is readAsBinaryString() deprecated