2016-11-15 93 views
1

我寫了一個下載功能,但是當文件寫在我的電腦上它是一個空文件(零字節長) 任何人都可以告訴我的原因?UWP下載文件與VB.Net

這是函數

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 

謝謝是一個很多

回答

1

我會寫一個下載功能,但是當文件被寫在我的電腦上它是一個 空文件(零字節lenght)

這是因爲你沒有開始下載操作。您只需created包含URI的下載操作將被安排。之後,您需要通過方法DownloadOperation.StartAsync開始下載操作。代碼如下:

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

此外,要在您的代碼中附加進度處理程序會好得多。有關如何處理進度處理和更多詳細信息,請參閱background transfer official sampledownload scenario。你也可以參考官方document

+0

非常感謝。我的錯誤是離開StartAsync行...... :-( –