コード例 #1
0
        private void OnUploadProgress(object sender, UploadProgressEventArgs e)
        {
            Dispatcher.BeginInvoke(() => {
                if (e.SessionId != sessionId)
                    return;

                if (statusView == null)
                    return;

                statusView.ProgressView.Value = (float)e.UploadedBytes / (float)e.TotalBytes;
            });
        }
コード例 #2
0
        private async Task<HttpWebResponse> UploadDataAsync(string sessionId, string fileName, Stream imageStream, Dictionary<string, string> parameters)
        {
            string boundary = "FLICKR_MIME_" + DateTime.Now.ToString("yyyyMMddhhmmss", System.Globalization.DateTimeFormatInfo.InvariantInfo);

            string authHeader = AnacondaCore.OAuthCalculateAuthHeader(parameters);
            byte[] dataBuffer = CreateUploadData(imageStream, fileName, parameters, boundary);

            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(new Uri("https://api.flickr.com/services/upload/"));
            req.Method = "POST";
            req.ContentType = "multipart/form-data; boundary=" + boundary;           
            if (!String.IsNullOrEmpty(authHeader))
            {
                req.Headers["Authorization"] = authHeader;
            }

            req.ContentLength = dataBuffer.Length;

            using (Stream reqStream = await req.GetRequestStreamAsync().ConfigureAwait(false))
            {
                int bufferSize = 32 * 1024;
                if (dataBuffer.Length / 100 > bufferSize) bufferSize = bufferSize * 2;

                int uploadedSoFar = 0;

                while (uploadedSoFar < dataBuffer.Length)
                {
                    reqStream.Write(dataBuffer, uploadedSoFar, Math.Min(bufferSize, dataBuffer.Length - uploadedSoFar));
                    uploadedSoFar += bufferSize;

                    if (PhotoUploadProgress != null)
                    {
                        UploadProgressEventArgs args = new UploadProgressEventArgs(sessionId, uploadedSoFar, dataBuffer.Length);
                        PhotoUploadProgress(this, args);
                    }
                }
                reqStream.Close();
            }

            // Invoke the API
            try
            {
                HttpWebResponse response = (HttpWebResponse)await req.GetResponseAsync().ConfigureAwait(false);
                return response;
            }
            catch (Exception e)
            {
                var we = e.InnerException as WebException;
                if (we != null)
                {
                    var resp = we.Response as HttpWebResponse;
                    var code = resp.StatusCode;
                    Debug.WriteLine("Status:{0}", we.Status);

                    return resp;
                }
                else
                    throw;
            }
        }