简体   繁体   English

如何将文件从IsolatedStorage上传到SkyDrive?

[英]How to upload a file from IsolatedStorage to SkyDrive?

I am trying to upload a text file, myFile.txt, to SkyDrive. 我正在尝试将文本文件myFile.txt上传到SkyDrive。 Below is the code: 下面是代码:

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 is global and has the value of: "folder.17ff6230f5f26b89.17FF6230F5F26B89!1533" FolderID是全局的,其值为:“ folder.17ff6230f5f26b89.17FF6230F5F26B89!1533”

The problem is in defining the second parameter of the BackgroundUploadAsync. 问题在于定义BackgroundUploadAsync的第二个参数。 How do I solve it, ie specify the URI of where "myFile.txt" IsolatedStorage file is? 我该如何解决,即指定“ myFile.txt” IsolatedStorage文件所在的URI?

Thanks, 谢谢,

You are passing 3 arguments in BackgroundUploadAsync where first one is FolderId, and second is path of the file, which is wrong check the documentation first , second argument takes the file name only not the Path of file and the third one is the stream of file. 您在BackgroundUploadAsync中传递了3个参数,其中第一个是FolderId,第二个是文件的路径,这是错误的, 首先检查文档 ,第二个参数仅使用文件名而不是文件的路径,第三个是文件流。

Also You can use UploadAsync instead BackgroundUploadAsync . 您也可以使用UploadAsync代替BackgroundUploadAsync

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

    var res = await client.UploadAsync(folderId, FileName, isfs, OverwriteOption.Overwrite);

Use this link for refrence. 使用此链接获取参考。

You are getting problem on upload, i guess it is because you are trying to upload file which is not in shared/transfer folder in isolated storage. 您在上传时遇到问题,我想这是因为您正在尝试上传不在独立存储中的共享/传输文件夹中的文件。 so just create file in shared/transfer and then try to upload file to sky drive. 因此只需在共享/传输中创建文件,然后尝试将文件上传到Sky Drive。

In order to upload the file from isolated storage using the BackgroundUploadAsync method, you need the correct URI to the file. 为了使用BackgroundUploadAsync方法从隔离的存储上载文件,您需要文件的正确URI。 The path to access files stored in isolated storage is /shared/transfers/ . 访问存储在独立存储中的文件的路径为/shared/transfers/ So, to pass the URI parameter to the method, preppend /shared/transfers/ to the file name. 因此,要将URI参数传递给方法,请在/shared/transfers/添加文件名。 That will be the valid URI. 那将是有效的URI。 See below: 见下文:

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

The other option is to use the UploadAsync method and attach the stream. 另一个选择是使用UploadAsync方法并附加流。

Thanks for all the pointers I got. 感谢我得到的所有指示。 Rewrote the function and it is like this: 重新编写函数,如下所示:

    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;
                }
            }
        }

The file was uploaded in the correct FolderID. 该文件已上传到正确的FolderID中。

Eitan 爱丹

To upload a file into Skydrive, the file should be saved inside "/shared/transfers/" folder in IsolatedStorage. 要将文件上传到Skydrive,应将文件保存在IsolatedStorage中的“ / shared / transfers /”文件夹中。 LiveConnectClient can only upload/download files from/to "/shared/transfers/". LiveConnectClient只能从“ / shared / transfers /”上载/下载文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM