Question

I am trying to upload a text file, myFile.txt, to 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"

The problem is in defining the second parameter of the BackgroundUploadAsync. How do I solve it, i.e. specify the URI of where "myFile.txt" IsolatedStorage file is?

Thanks,

Was it helpful?

Solution

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.

Also You can use UploadAsync instead BackgroundUploadAsync.

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

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

OTHER TIPS

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.

In order to upload the file from isolated storage using the BackgroundUploadAsync method, you need the correct URI to the file. The path to access files stored in isolated storage is /shared/transfers/. So, to pass the URI parameter to the method, preppend /shared/transfers/ to the file name. That will be the valid 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.

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.

Eitan

To upload a file into Skydrive, the file should be saved inside "/shared/transfers/" folder in IsolatedStorage. LiveConnectClient can only upload/download files from/to "/shared/transfers/".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top