Esempio n. 1
0
        void task_Completed(object sender, PhotoResult e)
        {
            if (e.ChosenPhoto == null)
            {
                return;
            }

            string uploadLocation = "/shared/transfers/Image" + DateTime.Now.Millisecond + ".jpg";
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += (o, args) =>
            {
                using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream stream = store.CreateFile(uploadLocation))
                    {
                        byte[] buffer = new byte[1 << 10];
                        int bytesRead;
                        while ((bytesRead = e.ChosenPhoto.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            stream.Write(buffer, 0, bytesRead);
                        }
                    }
                }
            };

            worker.RunWorkerCompleted += (o, args) =>
            {
                LiveConnectClient uploadClient = new LiveConnectClient(App.Session);
                uploadClient.BackgroundUploadCompleted +=
                    new EventHandler<LiveOperationCompletedEventArgs>(uploadClient_BackgroundUploadCompleted);

                string userState = "myUserState";  // arbitrary string to identify the request.
                uploadClient.BackgroundUpload(SelectedAlbum.ID, new Uri(uploadLocation, UriKind.RelativeOrAbsolute), userState);
            };

            worker.RunWorkerAsync();
        }