Download files on Windows Phone 10 or UWP using C#

I want to download the pdf file from server "http://che.org.il/wp-content/uploads/2016/12/pdf-sample.pdf" and save the same in local.

I using below code to achieve this. however I'm not able to download the pdf file.

I'm using the below code to download.

var uriBing = new Uri(@"http://che.org.il/wp-content/uploads/2016/12/pdf-sample.pdf"); try { // Create sample file; replace if exists. StorageFolder storageFolder = ApplicationData.Current.LocalFolder; StorageFile sampleFile = await storageFolder.CreateFileAsync("Prajesh.pdf", CreationCollisionOption.ReplaceExisting); //var sampleFile = await DownloadsFolder.CreateFileAsync("ManualFile.pdf", CreationCollisionOption.ReplaceExisting); var cli = new HttpClient(); var str = await cli.GetStreamAsync(uriBing); var dst = await sampleFile.OpenStreamForWriteAsync(); await str.AsInputStream().AsStreamForRead().CopyToAsync(dst);// AsStreamForRead().CopyTo(dst); } catch (Exception ex) { }

Is there any other approach to download and save file.

Tagged:

Posts

  • SivaShankarArumugamSivaShankarArumugam USMember ✭✭✭

    I using uwp with local machine as target.
    I have changed the as save method as below

    var uriBing = new Uri(@"http://che.org.il/wp-content/uploads/2016/12/pdf-sample.pdf"); StorageFolder storageFolder = ApplicationData.Current.LocalFolder; StorageFile sampleFile = await storageFolder.CreateFileAsync("Prajesh.pdf", CreationCollisionOption.ReplaceExisting); var cli = new HttpClient(); Byte[] bytes = await cli.GetByteArrayAsync(uriBing); IBuffer buffer = bytes.AsBuffer(); await Windows.Storage.FileIO.WriteBufferAsync(sampleFile, buffer);

    Now I'm able to wirte the pdf in localfolder.
    When I try to open the saved pdf using pdfjs it says
    "PDF.js v1.6.210 (build: 4ce2356)
    Message: Missing PDF "file:///C:/Users/ramanans/AppData/Local/Packages/698343db-d99e-4d49-b082-6c429844e50d_y1rwc9yf20874/LocalState/Prajesh.pdf"."

    Is my path is correct or I'm missing something.

    Same thing not working in windows phone 10 device as well as simulator

  • SivaShankarArumugamSivaShankarArumugam USMember ✭✭✭

    @NMackay do you have any idea on the above path issue

  • amirvenusamirvenus USMember ✭✭✭

    I am also having the same issue

Sign In or Register to comment.