简体   繁体   中英

save image get from couchdb server in phonegap app

I want save an image on filesystem device. The image was get from a couchdb server. The response from couchdb server is in MIME. I've parse the MIME string and I obtain the image data. this is the code:

function getImg(){
        $.ajax({
            url: "http://localhost:5984/testdb/10ef83b61f50e210cfe82620ef0016a3?attachments=true",
            success: function(data){
                var split = data.split("\r\n\r\n");
                var imageData = split[2].split("\r\n--")[0];
                var jsonData = split[1].split("\r\n--")[0];
                var jsonObj = JSON.parse(jsonData);

                //imageData is a string that contain image/jpeg read from MIME response
                window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem){

                fileSystem.root.getDirectory("PhoneGap Example", {create: true}, function(parent){

                    console.log("Directory create/open");
                    var d = new Date();
                    var n = d.getTime();
                    //new file name
                    var newFileName = n + ".jpeg";
                    fileSystem.root.getFile(newFileName, {create: true}, function(fileEntry){
                        console.log("create file image: " + fileEntry.name);

                        fileEntry.createWriter(function (writer){
                            writer.onwrite = function (evt){
                              largeImage.style.display = 'block';

                              // Show the captured photo
                              // The inline CSS rules are used to resize the image
                              //
                              largeImage.src = writer.fileName;
                            };

                            writer.write(imageData);
                        }, onWriterError);
                        fileEntry.moveTo(parent, newFileName, function(newFileEntry){
                            console.log("file move: " + newFileEntry.name);
                        }, onMoveFileError);
                    }, onResolveError);
                }, onGetDirectoryFail);
              }, onFileSystemError);
            }
        });
    }

the file was saved correctly but the content of file is not the image. I think because the imageData that I write in file is a string and not the binary data. I try to convert string in binary data with that function:

function str2ab(str) {
      var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
      var bufView = new Uint16Array(buf);
      for (var i=0, strLen=str.length; i<strLen; i++) {
        bufView[i] = str.charCodeAt(i);
      }
      return buf;
}

but I've the same issue. How can I do to save image correctly?

Maybe your data is base64 encoded and you need to decode it?

You could also have a look at the "Binary write Quick Example" on the Phonegap documentation for the binary write.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM