Exemplo n.º 1
0
        private async Task Upload(OAuth2Module module)
        {
            var statusBar = StatusBar.GetForCurrentView();

            statusBar.ProgressIndicator.Text = "Uploading";
            await statusBar.ProgressIndicator.ShowAsync();

            using (var client = new HttpClient())
                using (var content = new MultipartFormDataContent())
                {
                    client.DefaultRequestHeaders.Authorization = module.AuthenticationHeaderValue();

                    var fileContent = new StreamContent((await file.OpenAsync(FileAccessMode.Read)).AsStreamForRead());
                    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                    {
                        Name     = "\"image\"",
                        FileName = "\"" + file.Name + "\""
                    };
                    fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");

                    content.Add(fileContent);
                    HttpResponseMessage response = await client.PostAsync(uploadLocation[module.config.accountId], content);

                    string responseString = await response.Content.ReadAsStringAsync();

                    Debug.WriteLine(responseString);
                    await statusBar.ProgressIndicator.HideAsync();

                    await new MessageDialog("uploaded file " + (response.StatusCode != HttpStatusCode.OK ? "un" : "") + "successful").ShowAsync();
                }
        }
        public async void Upload(OAuth2Module module)
        {
            using (var client = new HttpClient())
                using (var content = new MultipartFormDataContent())
                {
                    button.IsEnabled  = false;
                    progress.IsActive = true;

                    client.DefaultRequestHeaders.Authorization = module.AuthenticationHeaderValue();

                    var fileContent = new StreamContent((await file.OpenAsync(FileAccessMode.Read)).AsStreamForRead());
                    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                    {
                        Name     = "\"image\"",
                        FileName = "\"" + file.Name + "\""
                    };
                    fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");

                    content.Add(fileContent);
                    HttpResponseMessage response = await client.PostAsync(new Uri("https://www.googleapis.com/upload/drive/v2/files"), content);

                    string responseString = await response.Content.ReadAsStringAsync();

                    Debug.WriteLine(responseString);
                    await new MessageDialog("uploaded file " + (response.StatusCode != HttpStatusCode.OK ? "un" : "") + "successful").ShowAsync();
                }

            file              = null;
            button.Content    = "Take Picture";
            button.IsEnabled  = true;
            progress.IsActive = false;
        }