0

我在将图像转换为 base64 字符串以通过 xml 传输时遇到一些问题。我在拍摄照片时将图像位置(完整路径)存储到数据库中,现在我正在尝试上传它。我正在尝试读取我在(results.rows.item(i).PictureFile) 中的文件,然后将其转换为base64。下面是我用来执行此操作的代码部分。任何人都可以做的任何帮助都会很棒。

   function submitPhoto(PhotoId)
{
    g_submitXML = '';
    var err = '';
    var errorsArr = $.makeArray(err);
    window.g_submitXML = '';
    window.g_submitXML = window.g_submitXML + '';
    window.g_submitXML = window.g_submitXML + '';
    window.g_submitXML = window.g_submitXML + '';
    console.log("successfully started the xml with: " + window.g_submitXML);

    var db = window.openDatabase("MobileData", "3.0", "MobilePhoneData", 1000000);
    db.transaction(function (tx) {
        tx.executeSql('SELECT * FROM MobilePhotos WHERE PhotoId = ' + PhotoId + '', 
          [], 
            function(tx, results) {
                console.log("HIT THE RESULTS PHOTO FUNCTION");
                var len = results.rows.length;
                console.log("Photo table: " + len + " rows found.");
                 for (var i = 0; i < len; i++)
                    {
                     window.g_submitXML = window.g_submitXML + '<EMBEDDED_FILE _Type="JPEG" _Name="Image_' + i + '"';
                     window.g_submitXML = window.g_submitXML + '_Extension=.jpg _EncodingType="Base64" _ImageOrientation="' + results.rows.item(i).PictureLocationDesc + '">';
                     window.g_submitXML = window.g_submitXML + '<DOCUMENT>';
                     var reader = new FileReader();
                 console.log(results.rows.item(i).PictureFile);
//I am having problems with the line below
                     window.g_submitXML = window.g_submitXML + reader.readAsDataURL(fileSystem.root.getFile(results.rows.item(i).PictureFile));
//I am having problems with the line above ^
                     window.g_submitXML = window.g_submitXML + '</DOCUMENT>';
                     window.g_submitXML = window.g_submitXML + '</EMBEDDED_FILE>';
                    }
        }, 
        console.log("ERROR"));
});


}

我已经在我遇到问题的行上方和下方发表了评论。

谢谢!

4

1 回答 1

1

我按照以下方式做到了。

检查这个答案

然后我使用以下代码使用网络服务上传图像。

//function to upload image
function uploadImage(ImagePath)
{
    try
    {
        var options = new FileUploadOptions();
        options.fileKey = "file";
        options.fileName = ImagePath;
        options.mimeType = "image/jpg";

        var params = new Object();    
        //You can set multiple params
        options.params = params;
        options.chunkedMode = false;
        var ft = new FileTransfer();
        var url = "My_WebService_URL";
        ft.upload(ImagePath, url, win, fail, options, false);
    } 
    catch (e) 
    {
        console.error("Error :"+e.message);
    }
}
//Success callback
function win(response) 
{
    alert("Image uploaded successfully!!");
}
//Failure callback
function fail(error) 
{
    alert("There was an error uploading image");
}

希望有帮助

于 2013-06-27T12:42:19.110 回答