Reputation: 1123
I am writing a WP8 (XAML & C#) application to read a text file from SkyDrive and save it into the IsolatedStorage.
It seems that the file is read, but the data I get is the description of the file rather than the content of the file.
The file name in SkyDrive is: "myFile1.txt"
The content of the file is: "this is a test file 1"
my code is below:
private async void btnDownload_Click( object sender, System.Windows.RoutedEventArgs e )
{
string fileID = "file.17ff6330f5f26b89.17FF6330F5F26B89!1644";
string filename = "myDownloadFile1.txt";
var liveClient = new LiveConnectClient( LiveHelper.Session );
// Download the file
liveClient.BackgroundDownloadAsync( fileID, new Uri( "/shared/transfers/" + filename, UriKind.Relative ) );
// Read the file
var FileData = await LoadDataFile< string >( filename );
Debug.WriteLine( "FileData: " + (string)FileData );
}
public async Task<string> LoadDataFile<T>( string fileName )
{
// Get a reference to the Local Folder
string root = ApplicationData.Current.LocalFolder.Path;
var storageFolder = await StorageFolder.GetFolderFromPathAsync( root + @"\shared\transfers" );
bool IsFileExist = await StorageHelper.FileExistsAsync( fileName, storageFolder );
if ( IsFileExist == true )
{
StorageFile storageFile = await storageFolder.GetFileAsync( fileName );
if ( storageFile != null )
{
// Open it and read the contents
Stream readStream = await storageFile.OpenStreamForReadAsync();
using ( StreamReader reader = new StreamReader( readStream ) )
{
string _String = await reader.ReadToEndAsync();
reader.Close();
return _String;
}
}
else
{
return string.Empty;
}
}
return string.Empty;
}
}
The data I am getting running this code is:
{
"id": "file.17ff6330f5f26b89.17FF6330F5F26B89!1644",
"from": {
"name": "Eitan Barazani",
"id": "17ff6330f5f26b89"
},
"name": "myFile1.txt",
"description": "",
"parent_id": "folder.17ff6330f5f26b89.17FF6330F5F26B89!1643",
"size": 23,
"upload_location": "https://apis.live.net/v5.0/file.17ff6330f5f26b89.17FF6330F5F26B89!1644/content/",
"comments_count": 0,
"comments_enabled": false,
"is_embeddable": true,
"source": "https://fculgq.bay.livefilestore.com/y2m0zkxi9kpb4orfYNSLSwst5Wy3Z7g6wDj7CM3B6wcOth9eA-gUflXeSCAAH_JWx2co72sgOTcGgvkwQGI3Gn5E1qXnRoKpVbsX_olRrB5gnCNIm8GrUrORco8_-je1cet/myFile1.txt?psid=1",
"link": "https://skydrive.live.com/redir.aspx?cid=17ff6330f5f26b89&page=view&resid=17FF6330F5F26B89!1644&parid=17FF6330F5F26B89!1643",
"type": "file",
"shared_with": {
"access": "Just me"
},
"created_time": "2013-07-15T16:29:10+0000",
"updated_time": "2013-07-15T16:29:10+0000"
}
I am not sure why I am not getting the data inside the file. Any idea what am I doing wrong?
Thanks,
Upvotes: 3
Views: 2971
Reputation: 1123
Two changes in my code:
private async void btnDownload_Click( object sender, System.Windows.RoutedEventArgs e )
{
string fileID = "file.17ff6230f5f26b89.17FF6230F5F26B89!1658";
string filename = "myDownloadFile1.txt";
var liveClient = new LiveConnectClient( LiveHelper.Session );
// Download the file
await liveClient.BackgroundDownloadAsync( fileID + "/Content", new Uri( "/shared/transfers/" + filename, UriKind.Relative ) );
// Read the file
var FileData = await LoadDataFile<string>( filename );
Debug.WriteLine( "FileData: " + (string)FileData );
}
public async Task<string> LoadDataFile<T>( string fileName )
{
// Get a reference to the Local Folder
string root = ApplicationData.Current.LocalFolder.Path;
var storageFolder = await StorageFolder.GetFolderFromPathAsync( root + @"\shared\transfers" );
bool isFileExist = await StorageHelper.FileExistsAsync( fileName, storageFolder );
if ( isFileExist )
{
// Create the file in the local folder, or if it already exists, just replace
StorageFile storageFile = await storageFolder.GetFileAsync( fileName );
if ( storageFile != null )
{
// Open it and read the contents
Stream readStream = await storageFile.OpenStreamForReadAsync();
using ( StreamReader reader = new StreamReader( readStream ) )
{
string _String = await reader.ReadToEndAsync();
reader.Close();
return _String;
}
}
return string.Empty;
}
return string.Empty;
}
}
The above code reads the file properly. I needed to add "/content" to the fileID. Also, await will not work in the emulator but works fine in the device ?! (this was new to me.....).
Upvotes: 0
Reputation: 49590
you're given a link in the metadata:
"source": "https://fculgq.bay.livefilestore.com/y2m0zkxi9kpb4orfYNSLSwst5Wy3Z7g6wDj7CM3B6wcOth9eA-gUflXeSCAAH_JWx2co72sgOTcGgvkwQGI3Gn5E1qXnRoKpVbsX_olRrB5gnCNIm8GrUrORco8_-je1cet/myFile1.txt?psid=1"
Use it to download the file, with for example HttpWebRequest
.
Here's a link from MSDN that explains what the Source parameter means - for example, for an audio file
Upvotes: 1
Reputation: 46
I think you need:
liveClient.BackgroundDownloadAsync( fileID+"/content", new Uri( "/shared/transfers/" + filename, UriKind.Relative ) );
i.e. after the fileID
string, you needed to add "/content"
in order to get the file content,
rather than the metadata.
Upvotes: 3