locked
write pdf metro app (base64)

    Question

  • i receive a base64 string which i decode. then i create a file with a pdf extension and after a write with a binary buffer in the file what i got from the decoded base64 string. but what i get is a blank pdf file. what am i doing wrong. please help.

    i am putting the code i'm using below:

                    

    function CreateFile(content, fileName) {
            try {
                var sampleFile;

                // Asynchronously create the file in the pictures folder.
                Windows.Storage.KnownFolders.documentsLibrary.createFileAsync(fileName, Windows.Storage.CreationCollisionOption.replaceExisting).then(function (file) {
                    sampleFile = file;
                }).done(function () {

                    // Create buffer
                    var buffer = Windows.Security.Cryptography.CryptographicBuffer.convertStringToBinary(
                    content, Windows.Security.Cryptography.BinaryStringEncoding['']
                    );

                    // Write buffer to file
                    Windows.Storage.FileIO.writeBufferAsync(sampleFile, buffer).done(function () {
                        console.log("---");
                    },
                    // Handle errors with an error function
                    function (error) {
                        console.log(error);
                    });
                });
            } catch (err) {
                console.log(err);
            }
        }

    my decoded string is something like this:

    %PDF-1.5
    %µµµµ
    1 0 obj
    <</Type/Catalog/Pages 2 0 R/Lang(es-MX) /StructTreeRoot 20 0 R/MarkInfo<</Marked true>>>>
    endobj
    2 0 obj
    <</Type/Pages/Count 2/Kids[ 3 0 R 17 0 R] >>
    endobj
    3 0 obj
    <</Type/Page/Parent 2 0 R/Resources<</XObject<</Image5 5 0 R>>/Font<</F1 6 0 R/F2 8 0 R/F3 10 0 R/F4 15 0 R>>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI] >>/MediaBox[ 0 0 594.96 842.04] /Contents 4 0 R/Group<</Type/Group/S/Transparency/CS/DeviceRGB>>/Tabs/S/StructParents 0>>
    endobj
    4 0 obj
    <</Filter/FlateDecode/Length 2543>>
    stream
    xœµ[Ínã8¾È;ð(
    Њø/-½pl§×Çvûg§wÂ

    Monday, June 17, 2013 5:39 PM

Answers

  • I saw this question a couple of times (receiving a PDF file through WinJS.xhr), I tried it myself with no luck, never tried your solution.

    The difference between the 2 methods is that WriteBytesAsync receives a ByteArray as parameter (which is what you get from the decoding), and WriteBufferAsync receives an IBuffer, not a byte array. The difference between an IBuffer and a byte array (at least what I undestood) is that the IBuffer is the result of the reading of a Stream, your byte array is just the result of a conversion.

    Another solution I found around was to use a Windows.Networking.BackgroundTransfer.BackgroundDownloader instead of WinJS.xhr.

    Here is the linked question in this forums and the answer.


    Tuesday, June 18, 2013 3:05 PM

All replies

  • How are you obtaining the "content" data that you are saving? Are you reading it from another file or obtaining it through WinJS.xhr?
    Monday, June 17, 2013 11:20 PM
  • I just figured it out. I get it from a WinJS.xhr call.

    Well first i just decode my base64 string and then save it into an bit array like this:

    var iBuffer = Windows.Security.Cryptography.CryptographicBuffer.decodeFromBase64String(items[0]);
    var fileBytes = Windows.Security.Cryptography.CryptographicBuffer.copyToByteArray(iBuffer);

    after that i just send the byte array and the file i want to write in. (i previously created it and store it in the file variable).

    writeFile(fileBytes, file);

    and after instead of using writebufferasync i used writeBytesAsync like this:

    function  writeFile(content, file) {

                    Windows.Storage.FileIO.writeBytesAsync(file, content).done(function () {

                        //some more code of mine
                    },
                    // Handle errors with an error function
                    function (error) {
                        console.log(error);
                    });

    }

    I don't really get the difference, could someone explain me?

    Tuesday, June 18, 2013 2:49 PM
  • I saw this question a couple of times (receiving a PDF file through WinJS.xhr), I tried it myself with no luck, never tried your solution.

    The difference between the 2 methods is that WriteBytesAsync receives a ByteArray as parameter (which is what you get from the decoding), and WriteBufferAsync receives an IBuffer, not a byte array. The difference between an IBuffer and a byte array (at least what I undestood) is that the IBuffer is the result of the reading of a Stream, your byte array is just the result of a conversion.

    Another solution I found around was to use a Windows.Networking.BackgroundTransfer.BackgroundDownloader instead of WinJS.xhr.

    Here is the linked question in this forums and the answer.


    Tuesday, June 18, 2013 3:05 PM