Ejemplo n.º 1
0
        public async Task <string> SendFileToServerAsync(string url, string filePath, string name,
                                                         Request request, CallbackOnFinished callbackOnFinish = null)
        {
            InitHttpClient();
            this.ConvertRequestToJson(request);
            MultipartFormDataContent form = new MultipartFormDataContent();

            IFile fileToSend = await FileSystem.Current.GetFileFromPathAsync(filePath);

            Stream fileToSendStream = await fileToSend.OpenAsync(FileAccess.Read);

            byte[] fileToSendBytesArray = StorageUtility.ConvertStreamInBytesArray(fileToSendStream);

            form.Add(new StringContent(API_KEY), "apikey");
            form.Add(new StringContent(JsonRequest), "data");
            form.Add(new ByteArrayContent(fileToSendBytesArray, 0, fileToSendBytesArray.Length),
                     name, filePath);
            Debug.WriteLine("Sending json : " + JsonRequest);
            HttpResponseMessage response = await httpClient.PostAsync(url, form);

            response.EnsureSuccessStatusCode();
            httpClient.Dispose();
            string serverResponse = response.Content.ReadAsStringAsync().Result;

            Debug.WriteLine("Server response = " + serverResponse);

            callbackOnFinish?.Invoke(serverResponse);

            return(serverResponse);
        }