locked
how to retrive downloaded files using winjs xhr

    Question

  • hi

       I have downloaded zip file using winJS xhr.how to track the downloaded zip file path because i should use that path to unzip(extract) zip files.

    or tell me how to transfer those files to pictures library

    Monday, December 2, 2013 12:40 PM

Answers

  • i tried this one but it downloads file with empty content.my function doesn't entering into try block it showing catch block error.


     
    var uriString = "http://someurl/imageset.zip";
    var fileName = "imageset";
    function DownloadFile(uriString, fileName) {
                try {
                    alert("started")
                    // Asynchronously create the file in the pictures folder.
                    Windows.Storage.KnownFolders.picturesLibrary.createFileAsync(fileName, Windows.Storage.CreationCollisionOption.generateUniqueName).done(function (newFile) {
                        //alert("test")
                        var uri = Windows.Foundation.Uri(uriString);
                        var downloader = new Windows.Networking.BackgroundTransfer.BackgroundDownloader();
                        
                        // Create a new download operation.
                        download = downloader.createDownload(uri, newFile);
    
                        // Start the download and persist the promise to be able to cancel the download.
                        promise = download.startAsync().then(function () { alert("success") }, error, function () { alert("progress")});
                    }, error);
                } catch (err) {
                    // displayException(err);
                    alert("error")
                }
            };
    
    



    Tuesday, December 3, 2013 4:22 AM
  • Hi Suganthi,

    To extract a zip file we can have at least two ways:

    1, Create a C# Runtime Component which contain the code for Unzip, by this you could execute C# code in JavaScript, read this for more information: http://msdn.microsoft.com/en-us/library/Windows/Apps/br230301.aspx

    2, Find some third party library, such as https://github.com/gildas-lormeau/zip.js

    And I think I've already answer you in your another post: http://social.msdn.microsoft.com/Forums/windowsapps/en-US/df40fa9b-1dcc-4fe9-877a-67c7e57b996c/how-to-download-zip-file-and-extract-zip-file-in-windows-81-store-app-using-javascript?forum=winappswithhtml5

    --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.

    Wednesday, December 4, 2013 2:40 AM
    Moderator

All replies

  • Hi Suganthi,

    BackgroundDownloader class is a easy way to download a file to a folder instead of using WinJS.xhr, xhr is design for some small files therefore you have to implement your own code to save the file to picture library.

            var download = null;
            var promise = null;
    
            function DownloadFile (uriString, fileName) {
                try {
                    // Asynchronously create the file in the pictures folder.
                    Windows.Storage.KnownFolders.picturesLibrary.createFileAsync(fileName, Windows.Storage.CreationCollisionOption.generateUniqueName).done(function (newFile) {
                        var uri = Windows.Foundation.Uri(uriString);
                        var downloader = new Windows.Networking.BackgroundTransfer.BackgroundDownloader();
    
                        // Create a new download operation.
                        download = downloader.createDownload(uri, newFile);
    
                        // Start the download and persist the promise to be able to cancel the download.
                        promise = download.startAsync().then(complete, error, progress);
                    }, error);
                } catch (err) {
                    displayException(err);
                }
            };

    --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.


    Tuesday, December 3, 2013 2:24 AM
    Moderator
  • i tried this one but it downloads file with empty content.my function doesn't entering into try block it showing catch block error.


     
    var uriString = "http://someurl/imageset.zip";
    var fileName = "imageset";
    function DownloadFile(uriString, fileName) {
                try {
                    alert("started")
                    // Asynchronously create the file in the pictures folder.
                    Windows.Storage.KnownFolders.picturesLibrary.createFileAsync(fileName, Windows.Storage.CreationCollisionOption.generateUniqueName).done(function (newFile) {
                        //alert("test")
                        var uri = Windows.Foundation.Uri(uriString);
                        var downloader = new Windows.Networking.BackgroundTransfer.BackgroundDownloader();
                        
                        // Create a new download operation.
                        download = downloader.createDownload(uri, newFile);
    
                        // Start the download and persist the promise to be able to cancel the download.
                        promise = download.startAsync().then(function () { alert("success") }, error, function () { alert("progress")});
                    }, error);
                } catch (err) {
                    // displayException(err);
                    alert("error")
                }
            };
    
    



    Tuesday, December 3, 2013 4:22 AM
  • Hi Suganthi,

    Can you tell me the what's the original error message? An empty file exist means at least the code to create empty file succeed.

    --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.

    Tuesday, December 3, 2013 6:33 AM
    Moderator
  • thanks james its working now
    Tuesday, December 3, 2013 7:05 AM
  • Hi james

               could you tell me how to unzip the zip files in pictures library with html5/javascript application

    Tuesday, December 3, 2013 10:46 AM
  • Hi Suganthi,

    To extract a zip file we can have at least two ways:

    1, Create a C# Runtime Component which contain the code for Unzip, by this you could execute C# code in JavaScript, read this for more information: http://msdn.microsoft.com/en-us/library/Windows/Apps/br230301.aspx

    2, Find some third party library, such as https://github.com/gildas-lormeau/zip.js

    And I think I've already answer you in your another post: http://social.msdn.microsoft.com/Forums/windowsapps/en-US/df40fa9b-1dcc-4fe9-877a-67c7e57b996c/how-to-download-zip-file-and-extract-zip-file-in-windows-81-store-app-using-javascript?forum=winappswithhtml5

    --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.

    Wednesday, December 4, 2013 2:40 AM
    Moderator