Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Javascript) make a File object with file path

I'm making a cordova-based application. Is it possible to make a File object with filepath?

What I am going to do is to take a picture and show the picture on a canvas. in android, it works fine but in ios6, the picture corrupted when drawing on a canvas because of mega-fixel problem on ios6.

then I found a plugin below

https://github.com/stomita/ios-imagefile-megapixel

the plugin need a File or Blob argument but I have only file URL. How can I get a File or Blob object from file URL?

my source is below

takePicture : function(onSuccess, onError) {
    options = {
        quality : 50,
        destinationType : navigator.camera.DestinationType.FILE_URI, //device returns File URL
        correctOrientation : true
    };
    navigator.camera.getPicture(onSuccess, onError, options);
}
onSuccess : function (photo_src) {
    $('#photo_hidden').attr('src', photo_src);
    setTimeout(function() {
    copyImage('photo_hidden', 'a04_image');
    }, 500);
}
copyImage : function (sourceId, targetId) {
    try{
    var source = document.getElementById(sourceId);
    var source_w = source.width;
    var source_h = source.height;
    var target = document.getElementById(targetId);
    var target_w = target.width;
    var target_h = target.height;
    target_h = Math.round(target_w / source_w * source_h);
    /////////////////////
    // I want to make a File object from source.src HERE!!
    /////////////////////
    //TODO this part will be changed to use plugin
    if (target.getContext) {
    var context = target.getContext('2d');
    context.drawImage(source, 0, 0, source_w, source_h, 0, 0, target_w, target_h);
    }
    //
    }catch (e) {
        console.log(e);
    }
}

If it is not possible, Any idea to make a File or Blob object is Welcome

Thanks in advance

like image 803
user2101676 Avatar asked Feb 23 '26 01:02

user2101676


1 Answers

I solved the problem by myself.

There is no way to make a File object without user's action. (need using <input type='file'> tag)

I looked into the plugin source.

and I found the argument doent't have to be File Object.

Image object can be used.

like image 111
user2101676 Avatar answered Feb 24 '26 13:02

user2101676