简体   繁体   中英

UWP Download File with VB.Net

i'd wrote a download function but when the file was written on my PC it's an empty file (zero byte lenght) Anyone can tell me the reason ?

This is the function

Public Shared Async Function StartDownload(ByVal InputUrl As String, ByVal FileName As String) As Task


    Dim MioPicker As FolderPicker = New FolderPicker()
    MioPicker.SuggestedStartLocation = PickerLocationId.Desktop
    MioPicker.ViewMode = PickerViewMode.Thumbnail
    MioPicker.FileTypeFilter.Add("*")
    Dim MioFolder As StorageFolder = Await MioPicker.PickSingleFolderAsync

    Dim source As New Uri(InputUrl)

    Dim destinationFile As StorageFile = Await MioFolder.CreateFileAsync(FileName, CreationCollisionOption.GenerateUniqueName)

    Dim downloader As New BackgroundDownloader()
    Dim download As DownloadOperation = downloader.CreateDownload(source, destinationFile)

End Function

Thank's a lots

try the following :

HandleDownloadAsync(download, true)

This will download the file in the backround and your programm does not have to wait for it to end . The code snipped is in C# but should work like in vb.

https://msdn.microsoft.com/library/windows/apps/br207154

i'd wrote a download function but when the file was written on my PC it's an empty file (zero byte lenght)

This is because you didn't start the download operation. You just created a download operation that includes a URI will be scheduled. After then you need to start the download operation by method DownloadOperation.StartAsync . Code as follows:

   Dim downloader As New BackgroundDownloader()
   Dim download As DownloadOperation = downloader.CreateDownload(source, destinationFile)
   Await download.StartAsync() 

Additionally, to attach a progress handler in your code will be much better. For how to handle a progress handle and more details please reference the download scenario of background transfer official sample . And you can also reference the official document .

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