Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中将文件URL转换为路径并发送到文件读取器_Javascript_Jquery_Cordova - Fatal编程技术网

在javascript中将文件URL转换为路径并发送到文件读取器

在javascript中将文件URL转换为路径并发送到文件读取器,javascript,jquery,cordova,Javascript,Jquery,Cordova,我正在使用cordova摄像头插件从gallery中捕获图片,下面是我的代码,现在我需要将URl传递给文件阅读器,当我将其传递给文件读取器时,它在成功回调中给我错误错误:Camera1358190195=TypeError:未能在“FileReader”上执行“readAsArrayBuffer”:参数1不是“Blob”类型即使“readAsBinarystring”不起作用也需要以下帮助是我的代码 //Cordova Camera Plugin function PicfromGalle

我正在使用cordova摄像头插件从gallery中捕获图片,下面是我的代码,现在我需要将URl传递给文件阅读器,当我将其传递给文件读取器时,它在成功回调中给我错误错误:Camera1358190195=TypeError:未能在“FileReader”上执行“readAsArrayBuffer”:参数1不是“Blob”类型即使“readAsBinarystring”不起作用也需要以下帮助是我的代码

 //Cordova Camera Plugin 

function PicfromGallery() {
var pictureSource = navigator.camera.PictureSourceType;
var destinationType = navigator.camera.DestinationType;
navigator.camera.getPicture(onSuccessEdituserProfileGallery, onFailEditProfileGallery, {
    quality: 50,
    sourceType: pictureSource.PHOTOLIBRARY,
    destinationType: destinationType.FILE_URI,
    targetWidth: 100,
    targetHeight: 100

});
}

function onSuccessEdituserProfileGallery(imageData) {
var smallImage
smallImage = document.getElementById('userProfileImage');
//EditUserProfileImageFilename(imageData);
smallImage.src = imageData;
var userPic = document.getElementById('EdituserProfileImage');
var file = new File(imageData);
OnFileImageEntry(file)
}

//File API
function OnFileImageEntry(file) {
var reader = new FileReader();
reader.onload = function (event) {
    var image = event.target.result;
    image.onload = function () {
       // need to get result
  }
  };
reader.readAsBinaryString(file);
 }
我找到了解决办法

您可以使用
window.resolveLocalFileSystemURL()
解析文件URI,而不是通过
新建文件(imageData)
创建文件对象

顺便说一句,
FileReader.readAsBinaryString()
不推荐使用。不要用它它已不在列表中

Mozilla仍然实现了
readAsBinaryString()
,并在中进行了描述

更多信息请阅读:。

我在中找到了一个解决方案

您可以使用
window.resolveLocalFileSystemURL()
解析文件URI,而不是通过
新建文件(imageData)
创建文件对象

顺便说一句,
FileReader.readAsBinaryString()
不推荐使用。不要用它它已不在列表中

Mozilla仍然实现了
readAsBinaryString()
,并在中进行了描述


更多信息请阅读:。

你找到解决方案了吗?@MahmoudFarahat我也遇到了同样的问题,并将我的答案发布在下面。试试看“你找到解决办法了吗?”马哈茂德法拉哈特我也遇到了同样的问题,并把我的答案贴在下面。试试看P
//Cordova Camera Plugin

function PicfromGallery() {
    var pictureSource = navigator.camera.PictureSourceType;
    var destinationType = navigator.camera.DestinationType;
    navigator.camera.getPicture(onSuccessEdituserProfileGallery, onFailEditProfileGallery, {
        quality: 50,
        sourceType: pictureSource.PHOTOLIBRARY,
        destinationType: destinationType.FILE_URI,
        targetWidth: 100,
        targetHeight: 100

    });
}

function onSuccessEdituserProfileGallery(imageData) {
    var smallImage;
    smallImage = document.getElementById('userProfileImage');
    smallImage.src = imageData;
    var userPic = document.getElementById('EdituserProfileImage');
    // var file = new File(imageData);
    OnFileImageEntry(imageData);  // Use the fileURI directly.
}

//File API
function OnFileImageEntry(file) {
    window.resolveLocalFileSystemURL(i, function (fileEntry) {
        fileEntry.file(function (file) {
            console.log('Now I have a file obj.');
            var reader = new FileReader();
            reader.onloadend = function (event) {
                var image = event.target.result;
                // Do something with the image
            };
            reader.readAsArrayBuffer(file);

        }, function (e) {
            console.log('Error getting file', e);
        });
    }, function (e) {
        console.log('Error resolving fs url', e);
    });
}