0

我正在尝试将文本文件 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?

谢谢,

4

5 回答 5

0

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

您也可以UploadAsync改用BackgroundUploadAsync.

    IsolatedStorageFileStream isfs = isf.OpenFile(FileName, FileMode.Open, FileAccess.Read);

    var res = await client.UploadAsync(folderId, FileName, isfs, OverwriteOption.Overwrite);
于 2013-07-12T04:43:11.913 回答
0

使用此链接作为参考。

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

于 2013-07-12T04:52:45.120 回答
0

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

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

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

于 2013-07-12T11:35:32.920 回答
0

感谢我得到的所有指点。重写了函数,它是这样的:

    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。

艾坦

于 2013-07-12T17:44:58.420 回答
0

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

于 2014-01-02T08:50:49.133 回答