简体   繁体   中英

UWP Save PDF File

I am currently working on A UWP App. This app has user manuals which are PDF Files. I need to be able to Open the PDF in the browser, and once opened save this to the downloads folder. The link to the PDF Is here: https://mysite.azurewebsites.net/Content/manuals/01-Introduction.pdf

Before i open this, I check if the File is already saved onto the device. If the file is not found, i will open the PDF, and need to automatically save it to the desired folder (which is just downloads at this moment). I already have the check in place as a try catch here:

        try {
            var ManualFile = await DownloadsFolder.CreateFileAsync("ManualFile.txt", CreationCollisionOption.FailIfExists);
        }
        catch
        {
            var uriBing = new Uri(@"https://mysite.azurewebsites.net/Content/manuals/01-Introduction.pdf");
            var success = await Windows.System.Launcher.LaunchUriAsync(uriBing);
        }

The Catch is where I then open the file in the browser, all i need now is to save this PDF to the Downloads Folder. The "ManualFile.txt" will be changed to the Name of the PDF which is saved, so once it is in the Downloads folder, the check should find the file. Any Help would be greatly Appreciated!

I think you just need to copy the file inside your program, instead of doing Save As with PDF browser. For example:

    var uriBing = new Uri(@"https://mysite.azurewebsites.net/manuals/01-Introduction.pdf");
    try 
    {
        var ManualFile = await DownloadsFolder.CreateFileAsync("ManualFile.pdf", CreationCollisionOption.FailIfExists);
        var cli = new HttpClient();
        var str = await cli.GetInputStreamAsync(uriBing);
        var dst = await ManualFile.OpenStreamForWriteAsync();
        str.AsStreamForRead().CopyTo(dst);
    }
    catch { }
    var success = await Windows.System.Launcher.LaunchUriAsync(uriBing));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM