Search code examples
javascripttizen

tizen upload file to a server


I'm developing an app like shazam; I managed to record, but when I upload the audio file using the blob the data on the server side the was not same (I can't play the audio file).

tizen.filesystem.resolve(
    'file:///opt/usr/media/test.aac',
    function(dir) {
        documentsDir = dir;

        if (dir != null) {
            dir.openStream(
                "rw",
                function(fs) {
                    var bt = fs.readBytes(dir.fileSize);

                    var blob = new Blob(bt, {
                        type: 'audio/mpeg'
                    });
                    var formData = new FormData();
                    formData.append("__VIEWSTATE", "jjj=");
                    formData.append("FileUploadControl", blob, "mmm.aac");
                    formData.append("UploadButton", "Upload");
                    client.open("post", "/page", true);

                    client.send(formData);
                    fs.close();
                },
                function(e) {
                    console.log("Error " + e.message);
                }
            );
        }

    },
    function(e) {
        console.log("Error" + e.message);
    }, "rw"
);

Solution

  • Replace

    var blob = new Blob(bt, {type: 'audio/mpeg'});
    

    with

    var array = $.map(bt, function(value, index) {  return [value + "\n"];  });
    var blob = new Blob(array, {type : 'audio/mpeg'});
    

    You may leave the line endings "\n" out, depending on your needs, of course. In my case, I iterated over every line in the file and converted back to text values using PHP's chr() function. Furthermore, I am working with plain text, so you might need a little bit tweaking.