Пример #1
0
        public static async Task<string> UploadImageAsync(Image bitmap)
        {
            var memoryStream = new MemoryStream();  
            bitmap.Save(memoryStream, ImageFormat.Png);
            memoryStream.Seek(0, SeekOrigin.Begin);

            var streamContent = new StreamContent(memoryStream);
            streamContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/png");

            var multipartDataContent = new MultipartFormDataContent
            {
                {streamContent, "shot", "screenshot.png"}
            };

            var responseMessage = await _httpClient.PostAsync(API_URL, multipartDataContent);
            if (!responseMessage.IsSuccessStatusCode)
                return null;

            var responseString = await responseMessage.Content.ReadAsStringAsync();
            var json = _javaScriptSerializer.Deserialize<dynamic>(responseString);

            streamContent.Dispose();
            multipartDataContent.Dispose();
            if (json["result"] == ":(")
            {
                Console.WriteLine("Error!");
                Console.WriteLine(json["error"]["code"]);
                Console.WriteLine(json["error"]["message"]);
            }

            return json["link"];
        }
        public void UploadExtensionLibraryAsync(string selectedFileName, byte[] fileContents, ObservableCollection<string> assemblies, object userState)
        {
            Uri uri = CreateRestRequest("Extensions/Upload");

            var assembliesString = assemblies != null ? string.Join(",", assemblies) : null;
            var content = new MultipartFormDataContent();
            var ms = new MemoryStream(fileContents);
            var fileContent = new StreamContent(ms);

            // Specify the content disposition and content type - without this the form data will not
            // be included in the Request object in .NET 2.0 app pools
            fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
            {
                Name = "\"files\"",
                FileName = "\"" + selectedFileName + "\""
            };
            fileContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-silverlight-app");
            content.Add(fileContent);
            
            var stringContent = new StringContent(assembliesString);

            // Need to specify the content disposition and content type for .NET 2.0 compatibility here, too
            stringContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
            {
                Name = "\"assemblies\""
            };
            stringContent.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
            content.Add(stringContent);

            var client = new HttpClient();
            client.PostAsync(uri, content).ContinueWith(t =>
            {
                stringContent.Dispose();
                fileContent.Dispose();
                ms.Dispose();
                content.Dispose();

                if (t.IsCanceled)
                    return;

                if (t.Exception != null)
                {
                    var errorDisplay = new ErrorDisplay();

                    // Attempt to get the stack trace with IL offsets
                    string stackTraceIL = t.Exception.StackTraceIL();

                    ErrorData data = new ErrorData()
                    {
                        Message = t.Exception.Message,
                        StackTrace = !string.IsNullOrEmpty(stackTraceIL) ? stackTraceIL :
                            t.Exception.StackTrace
                    };

                    errorDisplay.DataContext = data;

                    // Size the error UI
                    double width = Application.Current.RootVisual.RenderSize.Width * 0.67;
                    errorDisplay.Width = width > errorDisplay.MaxWidth ? errorDisplay.MaxWidth : width;
                    errorDisplay.Completed += (o, a) => BuilderApplication.Instance.HideWindow(errorDisplay);

                    // Show the error
                    BuilderApplication.Instance.ShowWindow(Strings.ErrorOccured, errorDisplay, false, null, null);
                }

                if (UploadExtensionLibraryCompleted != null)
                {
                    UploadExtensionLibraryCompleted(this, new UploadExtensionLibraryCompletedEventArgs()
                    {
                        Error = t.Exception,
                        UserState = userState,
                    });
                }

            }, TaskScheduler.FromCurrentSynchronizationContext());
        }