Lepozepo / cloudinary

MIT License
94 stars 42 forks source link

Upload blob #102

Closed odesey closed 8 years ago

odesey commented 8 years ago

I am trying to extract some images from .epub files and then save them to Cloudinary but I am not having any luck. Here is what I've done thus far:

Using epub.js I load the .epub file and read the image data:


 const book = ePub('path_to_file.epub');
 const cover = book.coverUrl();
/* cover._result returns this:
"blob:http://localhost:3000/6433bd4e-13dc-462b-b7be-e9654ad06c18"
*/

I can manually assign cover._url to an image source and the image renders as expected:

  $('#bookImage').attr('src', cover._result)

I have tried to upload the cover._result , "blob:http://localhost:3000/6433bd4e-13dc-462b-b7be-e9654ad06c18" like so:

          Cloudinary.upload( cover._result,{
            folder: 'Books',
            resource_type: 'raw',
          },function(err, res){ 
            if (err){ 
              console.log(err); 
            } else { 
              console.log(res);
               } 
             });

and I get no response

I also tried this:

const myFile = new File([cover._result], "new_file.jpg", {type: "image/jpg"}); 
Cloudinary.upload( myFile,{...})

The file uploads, BUT its garbage data and not readable

Lastly I tried converting the file to base64 like so:


 var toDataUrl = function(cover, callback) {
  var xhr = new XMLHttpRequest();
  xhr.responseType = 'blob';
  xhr.onload = function() {
    var reader = new FileReader();
    reader.onloadend = function() {
      callback(reader.result);
    }
    reader.readAsDataURL(xhr.response);
  };
  xhr.open('GET', cover);
  xhr.send();
}

/******************/
      toDataUrl (cover._result, function( base64Img ) {
        Cloudinary.upload( base64Img,{
          folder: 'Books',
           resource_type: 'raw',
         },function(err, res){ 
            if (err){ 
              console.log(err); 
            } else { 
              console.log(res);
            } 
        });
    })

I get no response from the Cloudinary.upload function.

So my question, is this even possible with the package? If so what am I missing?

Thanks.

odesey commented 8 years ago

Solved this based on info found here #19

Changed this:


      toDataUrl (cover._result, function( base64Img ) {
        Cloudinary.upload( base64Img,{
          folder: 'Books',
           resource_type: 'raw',
         },function(err, res){ 
            if (err){ 
              console.log(err); 
            } else { 
              console.log(res);
            } 
        });
    })

to this:


      toDataUrl (cover._result, function( base64Img ) {
        Cloudinary._upload_file( base64Img,{
          folder: 'Books',
         },function(err, res){ 
            if (err){ 
              console.log(err); 
            } else { 
              console.log(res);
            } 
        });
    })

Problem solved.

This should probably be in the documentation.

Lepozepo commented 8 years ago

Awesome! Glad you solved it, I'll add it to the docs.