javascriptwindowsuwpstoragefilestoragefolder

How to replace a file that is currently opened?


I am actually working with pdf documents in my app. I am storing the pdf file that is to be readed in Local folder of the app. This file is used through out the app, so I think that this file remains open through out the app. When I want to replace the file with a new one it gives me error and I think the reason is that the file is in use. I cant find any option to close this file. Is there anyway to close a file opened from loadFromFileAsync() method of pdfDocument class? or is there any other way to replace a file that is in use.

var loc = Windows.Storage.ApplicationData.current.localFolder;
var picker = new Windows.Storage.Pickers.FileOpenPicker();
var pdf;

function newFile() {
    picker.fileTypeFilter.append(".pdf");
    picker.pickSingleFileAsync().then(
        function (f) {
            f.copyAsync(loc, "main.pdf", Windows.Storage.NameCollisionOption.replaceExisting).done(
                function (f1) {
                    Windows.Data.Pdf.PdfDocument.loadFromFileAsync(f1).then(
                        function (file) {
                            pdf = file;
                            Windows.UI.Popups.MessageDialog("loaded " + pdf).showAsync();
                            document.getElementById("btn").onclick = newFile;
                            });
                        },
                        function (e) {
                            Windows.UI.Popups.MessageDialog("Error: " + e).showAsync();
                        });
                });
    }
newFile();

A file named as "main.pdf" is stored in the Local folder of the app. The code above is re-creating the problem that is occuring in my application. So, in the code above, When the application is launched, the newFile() method is called and replaces the "main.pdf" file in the local folder with a new "main.pdf" file. But when the newFile() method is called again in the application it gives me an unspecified error in replacing the "main.pdf" file.

What I think is that, the file that is to be replaced is being operated by the PdfDocument class and that is the cause of error, because when I dont load this file from loadFromFileAsync() method of PdfDocument class, the newFile() method works fine for the second time as well. what is the way to get this work done?


Solution

  • You declare the pdf variable outside the newFile(), but this variable reference the pdf file in newFile() method. When the calling to newFile() method completed, this variable has not been released.

    To solve your issue, you could declare this pdf variable in newFile() method.

    function newFile() {
        var pdf;
        picker.fileTypeFilter.append(".pdf");
        picker.pickSingleFileAsync().then(
            function (f) {
                f.copyAsync(loc, "main.pdf", Windows.Storage.NameCollisionOption.replaceExisting).done(
                    function (f1) {
                        Windows.Data.Pdf.PdfDocument.loadFromFileAsync(f1).then(
                            function (file) {
                                pdf = file;
                                Windows.UI.Popups.MessageDialog("loaded " + pdf).showAsync();
    
                            });
                    },
                    function (e) {
                        Windows.UI.Popups.MessageDialog("Error: " + e).showAsync();
                    });
            });
    }