locked
how to get the base64 data of a image file using javascript RRS feed

  • Question

  • i want to upload a file, so i first use Windows.Storage.Pickers.FileOpenPicker to get a image file such as .jpg file, but i don't know how to get the base64 data of the file content

    var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
            openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.list;
            openPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
            openPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg"]);
            openPicker.pickSingleFileAsync().then(function (file) {
                if (file) {
                    // i want to get the base64 data of the image here                
                } else {

                }
            });

    thank you very much!

    Monday, February 13, 2012 9:11 AM

Answers

  • finally i find the way to fix the problem, like this:

    var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
            openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.list;
            openPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
            openPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg"]);
            openPicker.pickSingleFileAsync().then(function (file) {
                if (file) {
                    file.openAsync(Windows.Storage.FileAccessMode.read).then(function (stream) {
                        var inputStream = stream.getInputStreamAt(0);
                        var reader = new Windows.Storage.Streams.DataReader(inputStream);
                        var size = stream.size;
                        if (size > 0) {
                            reader.loadAsync(size).then(function () {
                                var b = reader.readBuffer(size);
                                var s = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(b);
                            });
                        } 
                    });
                } else {
                }
            });

    • Marked as answer by zhrongvista Monday, February 13, 2012 10:08 AM
    Monday, February 13, 2012 10:08 AM