Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.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中将文件加载到字节数组中_Javascript_Jquery_Cordova_Jquery Mobile - Fatal编程技术网

如何在JavaScript中将文件加载到字节数组中

如何在JavaScript中将文件加载到字节数组中,javascript,jquery,cordova,jquery-mobile,Javascript,Jquery,Cordova,Jquery Mobile,我正在用cordova开发一个移动应用程序。应用程序需要在某个时刻离线,这意味着它必须在内部数据库中有数据 function(tx, row){ var query = "INSERT INTO Restaurants (name, address, logo, image) values (?,?,?,?)"; var logo = null; // I need to fill it with the logo var picture = null; // I nee

我正在用cordova开发一个移动应用程序。应用程序需要在某个时刻离线,这意味着它必须在内部数据库中有数据

function(tx, row){
    var query = "INSERT INTO Restaurants (name, address, logo, image) values (?,?,?,?)";
    var logo = null; // I need to fill it with the logo
    var picture = null; // I need to fill it with the picture
    tx.executeSql(query, [row["nombre"], row["address"],logo,picture], onSuccess, onError);
});
数据由一组字段组成,其中一些字段是指向文件的链接。例如:

var restaurant = {
   name: 'Restaurant X'
   address: 'So far'
   logo: http://www.restaurantX.com/logo.png
   image: pics/restaurant-x.jpg
}
当用户决定离线时,我们应该从数据库中提取所有相关信息(此点已清除),并下载所有相关图像(这就是我被卡住的地方)。在这一点上,我认为有两种选择,第一种是将所有文件作为文件或数据库中的数据下载到手机上,然而,项目负责人说,必须将图像作为blob保存在数据库中

function(tx, row){
    var query = "INSERT INTO Restaurants (name, address, logo, image) values (?,?,?,?)";
    var logo = null; // I need to fill it with the logo
    var picture = null; // I need to fill it with the picture
    tx.executeSql(query, [row["nombre"], row["address"],logo,picture], onSuccess, onError);
});
我一直在寻找一个将文件内容转换为字节数组的函数,以便将其保存到数据库中,但我需要一个同步函数,因为在将行保存到数据库中之前,我必须等待图片

function(tx, row){
    var query = "INSERT INTO Restaurants (name, address, logo, image) values (?,?,?,?)";
    var logo = null; // I need to fill it with the logo
    var picture = null; // I need to fill it with the picture
    tx.executeSql(query, [row["nombre"], row["address"],logo,picture], onSuccess, onError);
});

如果这个问题过于简单,我很抱歉,我对JavaScript几乎一无所知。

如果您的图像序列化例程是异步的,并且希望在序列化成功时存储它,那么您肯定应该看看promises。关于您可以检查的承诺的更多详细信息

,但我需要它同步,因为我需要将值保存到数据库中
听起来像是一个XY问题:它与异步和同步有什么关系?向我们展示您的尝试,也许我们可以为您指出正确的方向。@A.Wolff我明白您的意思。我会尽量把我的问题说得更具体一些。