如何将文件从IsolatedStorage上载到SkyDrive

本文关键字:上载 SkyDrive IsolatedStorage 文件 | 更新日期: 2023-09-27 18:22:11

我正在尝试将文本文件myFile.txt上载到SkyDrive。以下是代码:

private async void btnUpload_Click( object sender, System.Windows.RoutedEventArgs e )
{
    Client = new LiveConnectClient( _session );
    string filename = "myFile.txt";
    var isolatedstorageFile= await ApplicationData.Current.LocalFolder.CreateFileAsync( filename, CreationCollisionOption.ReplaceExisting );
    using ( StreamWriter writer = new StreamWriter( await isolatedstorageFile.OpenStreamForWriteAsync() ) )
    {
        // convert to string
        var _String = Serialize( "this is a test file" );
        await writer.WriteAsync( _String );
    }
    await Client.BackgroundUploadAsync( FolderID, new Uri( isolatedstorageFile.Path ), OverwriteOption.Overwrite );
}

FolderID是全局的,其值为:"folder.17ff6230f5f26b89.17ff6230f5f26b89!1533"

问题在于定义BackgroundUploadAsync的第二个参数。我该如何解决它,即指定"myFile.txt"IsolatedStorage文件所在位置的URI?

谢谢,

如何将文件从IsolatedStorage上载到SkyDrive

您在BackgroundUploadAsync中传递了3个参数,其中第一个是FolderId,第二个是文件的路径,这是错误的。请首先检查文档,第二参数只使用文件名而不使用文件的路径。第三个是文件流。

也可以使用UploadAsync而不是BackgroundUploadAsync

    IsolatedStorageFileStream isfs = isf.OpenFile(FileName, FileMode.Open, FileAccess.Read);
    var res = await client.UploadAsync(folderId, FileName, isfs, OverwriteOption.Overwrite);

使用此链接进行引用。

你在上传时遇到了问题,我想是因为你试图上传的文件不在隔离存储的共享/传输文件夹中。所以只需在共享/传输中创建文件,然后尝试将文件上传到sky驱动器。

为了使用BackgroundUploadAsync方法从隔离存储上传文件,您需要文件的正确URI。访问存储在隔离存储中的文件的路径是/shared/transfers/。因此,要将URI参数传递给该方法,请在文件名前面加上/shared/transfers/。这将是有效的URI。见下文:

await Client.BackgroundUploadAsync(FolderID, new Uri("/shared/transfers/" + fileName, UriKind.Relative), OverwriteOption.Overwrite );

另一种选择是使用UploadAsync方法并附加流。

谢谢你给我的所有指针。重写函数,它是这样的:

    private async void btnUpload_Click( object sender, System.Windows.RoutedEventArgs e )
        {
        string filename = "myFile.txt";
        StorageFolder folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync( "Shared", CreationCollisionOption.OpenIfExists );
        folder = await folder.CreateFolderAsync( "transfers", CreationCollisionOption.OpenIfExists );
        var isolatedstorageFile= await folder.CreateFileAsync( filename, CreationCollisionOption.ReplaceExisting );
        using ( StreamWriter writer = new StreamWriter( await isolatedstorageFile.OpenStreamForWriteAsync() ) )
            {
            // convert to string
            var _String = Serialize( "this is a test file" );
            await writer.WriteAsync( _String );
            }
        await LiveHelper.Client.BackgroundUploadAsync( FolderID, new Uri( "/shared/transfers/" + filename, UriKind.Relative ), OverwriteOption.Overwrite );
        }

    private static string Serialize( object objectToSerialize )
        {
        using ( MemoryStream _Stream = new MemoryStream() )
            {
            try
                {
                var _Serializer = new DataContractJsonSerializer( objectToSerialize.GetType() );
                _Serializer.WriteObject( _Stream, objectToSerialize );
                _Stream.Position = 0;
                StreamReader _Reader = new StreamReader( _Stream );
                return _Reader.ReadToEnd();
                }
            catch ( Exception e )
                {
                Debug.WriteLine( "'n******** Serialize:" + e.Message );
                return string.Empty;
                }
            }
        }

文件已上载到正确的FolderID中。

艾坦

要将文件上传到Skydrive,文件应保存在IsolatedStorage的"/shared/transfers/"文件夹中。LiveConnectClient只能从/向"/shared/transfers/"上传/下载文件。