我正在尝试将文本文件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的第二个参数。我该如何解决它,即指定where" myFile.txt"的URI。 IsolatedStorage文件是?
谢谢,
答案 0 :(得分:0)
你在BackgroundUploadAsync
传递了3个参数,其中第一个是FolderId,第二个是文件的路径,这是错误的检查documentation first,第二个参数只取文件名而不是路径文件,第三个是文件流。
此外,您可以使用UploadAsync
代替BackgroundUploadAsync
。
IsolatedStorageFileStream isfs = isf.OpenFile(FileName, FileMode.Open, FileAccess.Read);
var res = await client.UploadAsync(folderId, FileName, isfs, OverwriteOption.Overwrite);
答案 1 :(得分:0)
使用this link作为参考。
您在上传时遇到问题,我想这是因为您尝试上传不在隔离存储中的共享/传输文件夹中的文件。所以只需在共享/传输中创建文件,然后尝试将文件上传到天空驱动器。
答案 2 :(得分:0)
要使用BackgroundUploadAsync
方法从隔离存储上载文件,您需要文件的正确URI。访问存储在独立存储中的文件的路径为/shared/transfers/
。因此,要将URI参数传递给方法,请将/shared/transfers/
预先附加到文件名。这将是有效的URI。见下文:
await Client.BackgroundUploadAsync(FolderID, new Uri("/shared/transfers/" + fileName, UriKind.Relative), OverwriteOption.Overwrite );
另一种选择是使用UploadAsync
方法并附加流。
答案 3 :(得分: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。
埃坦
答案 4 :(得分:0)
要将文件上传到Skydrive,该文件应保存在IsolatedStorage的“/ shared / transfers /”文件夹中。 LiveConnectClient只能从/向“/ shared / transfers /".
上传/下载文件