2

我正在开发一个 Windows 运行时通用应用程序。

我需要将我在应用程序中使用的数据库上传到用户的 OneDrive?

FileNotFound出现异常。但我知道路径是正确的。

由于我引用了 sqlite 文件,因此它显示了异常。如果我参考 txt 文件,上传过程会顺利进行。

var authClient = new LiveAuthClient();
var authResult = await authClient.LoginAsync(new string[] { "wl.skydrive", "wl.skydrive_update" });
if (authResult.Session != null)
{
    var liveConnectClient = new LiveConnectClient(authResult.Session);
    var FileToUpload = await ApplicationData.Current.LocalFolder.GetFileAsync("text.sqlite");//exception occurs here        
    var FileToUpload = await ApplicationData.Current.LocalFolder.GetFileAsync("text.txt");//no exception for txt files

    var folderData = new Dictionary<string, object>();
    folderData.Add("name", "Folder") 
    LiveOperationResult operationResult = await liveConnectClient.PostAsync("me/skydrive", folderData);

    LiveUploadOperation uploadOperation = await liveConnectClient.CreateBackgroundUploadAsync(folderId, "filename", FileToUpload, OverwriteOption.Overwrite);
    LiveOperationResult uploadResult = await uploadOperation.StartAsync();
    HandleUploadResult(uploadResult);
}
4

1 回答 1

1

首先,您应该使用后台传输任务来移动那么大的文件并处理用户在传输过程中接到电话的情况。后台任务是关键。

http://code.msdn.microsoft.com/windowsapps/Background-Transfer-Sample-d7833f61/sourcecode?fileId=52027&pathId=1495533284

有点像这样。

BackgroundUploader uploader = new BackgroundUploader(); 
UploadOperation upload = uploader.CreateUpload(uri, file); 
await HandleUploadAsync(upload, true); 

但在你这样做之前,你需要假设用户将操纵数据库。因此,您应该在开始上传之前复制数据库文件。再次,代码:

var sourceFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
var sourceFile = await sourceFolder.CreateFileAsync("database", 
    Windows.Storage.CreationCollisionOption.OpenIfExists);
var targetFolder = await sourceFolder.CreateFolderAsync("~", 
    Windows.Storage.CreationCollisionOption.OpenIfExists);
var targetFile = await targetFolder.CreateFileAsync(sourceFile.Name,
    Windows.Storage.CreationCollisionOption.ReplaceExisting);
await sourceFile.MoveAndReplaceAsync(targetFile);

这不仅是一个好主意,我觉得它会纠正你遇到的问题。请记住,您始终可以查询并询问是否完成了转移(您不是在做两个吗)。

祝你好运!

于 2014-06-04T15:16:31.817 回答