使用Javascript创建一个带有文件路径的File对象。

3
我正在制作一个基于cordova的应用程序。 是否可以使用文件路径创建一个File对象?
我的目标是拍照并在画布上展示图片。 在Android中,这很好用,但在iOS6中,由于iOS6上的mega-fixel问题,在画布上绘制时图片会出现损坏。
然后我找到了下面的插件。

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

这个插件需要一个文件或Blob参数,但我只有文件URL。如何从文件URL获取File或Blob对象?

我的源代码如下:

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);
    }
}

如果不可能,欢迎提供任何制作文件或 Blob 对象的想法。
提前感谢。
1个回答

1
我自己解决了这个问题。
没有办法在没有用户操作的情况下创建一个文件对象。 (需要使用标签)
我查看了插件源代码。
我发现参数不一定是文件对象。
可以使用图像对象。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接