コード例 #1
0
        void UploadConfigCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                // TODO: 记录错误...
                MessageBox.Show("现在连接不上服务,请稍后再试.");
            }
            else
            {
                try
                {
                    // 返回xml文件,包括Blob的SAS.
                    XDocument resultXDoc = XDocument.Parse(e.Result);
                    this._storyID = resultXDoc.Root.Attribute("ID").Value;
                    var photoElements = resultXDoc.Root.Elements("Photo");

                    lock (this._lockObject)
                    {
                        this._allPhotoCount = photoElements.Count();
                    }

                    // 创建一个背后的线程,等待所有图片被上传.
                    // 然后提交.
                    Thread thread = new Thread(new ThreadStart(this.WaitUntilAllPhotosUploaded));
                    thread.Start();

                    foreach (var photoElement in photoElements)
                    {
                        string name    = photoElement.Attribute("Name").Value;
                        string blobUri = photoElement.Attribute("Uri").Value;

                        // 找到当前story的所有图片.
                        Photo photo = App.MediaCollection.Where(p => p.Name == name).FirstOrDefault();
                        if (photo == null)
                        {
                            throw new InvalidOperationException("找不到图片.");
                        }
                        if (photo.ResizedImageStream == null)
                        {
                            photo.ResizedImageStream = BitmapHelper.GetResizedImage(photo.Name);
                        }

                        // 上传图片到blob.
                        photo.ResizedImageStream.Position = 0;

                        RetryPolicy policy = new RetryPolicy(blobUri);
                        policy.RequestAddress = blobUri;
                        policy.Initialize     = new Action(() =>
                        {
                            policy.Request.Method = "PUT";
                        });
                        policy.RequestCallback = new AsyncCallback((requestStreamResult) =>
                        {
                            Stream requestStream = policy.Request.EndGetRequestStream(requestStreamResult);
                            byte[] buffer        = new byte[photo.ResizedImageStream.Length];
                            photo.ResizedImageStream.Position = 0;
                            photo.ResizedImageStream.Read(buffer, 0, buffer.Length);
                            requestStream.Write(buffer, 0, buffer.Length);
                            requestStream.Close();
                        });

                        policy.ResponseCallback = new AsyncCallback((responseResult) =>
                        {
                            HttpWebResponse response = (HttpWebResponse)policy.Request.EndGetResponse(responseResult);
                            if (response.StatusCode != HttpStatusCode.Created)
                            {
                                throw new InvalidOperationException("上传失败");
                            }
                            lock (this._lockObject)
                            {
                                this._uploadedPhotoCount++;
                            }
                        });
                        policy.MakeRequest();
                    }
                }
                catch
                {
                    // TODO: 记录错误...

                    lock (this._lockObject)
                    {
                        this._uploadFailed = true;
                    }
                    MessageBox.Show("现在无法上传,请稍后再试.");
                }
            }
        }