locked
Problem with writing content into pdf file

    Question

  • I am downloading the file from server using WInJS.xhr. and the content-type is application/pdf. Response is also getting properly from server. file is created but when I am opening the file it is showing me the error that "content may be damaged or not properly decoded".  What is missing here. Here is my code.

    WinJS.xhr({ url: url,responseType:'arraybuffer', user: 'user', password: 'pass' })
               .done(
                   function (request) {
                      
                       var arrayResponse = request.response;
                       var buf = new ArrayBuffer(arrayResponse.length);
                      var ints = new Uint8Array(buf);
                       Windows.Storage.KnownFolders.documentsLibrary.createFileAsync("newfile.pdf", Windows.Storage.CreationCollisionOption.generateUniqueName).done(function (newFile) {
                           Windows.Storage.FileIO.writeBytesAsync(newFile, ints);
                         
                       }, function onerror() {
                           console.log("Error in saving the file");
                       });




                   });

    Friday, September 19, 2014 2:56 PM

Answers

  • Hi Asha,

    When I apply your code on my project, I can see the ints here is always 0, could you confirm with your side?

    If a file size is 0, definitely "content may be damaged or not properly decoded".  I would recommend you to use following code instead:

    var ints = new Uint32Array(arrayResponse.byteLength / 4);
    //var buf = new ArrayBuffer(arrayResponse.length);
    //var ints = new Uint8Array(buf);

    Then you should be able to see another exception: "Type mismatch", I'm not sure but I guess it could be something wrong with the responseType?

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, September 22, 2014 6:26 AM
    Moderator