Upload an image via httpclient

1.8k views Asked by At

I have this code that uploads data

public async Task<int> UploadAllDeposit()
{
    tableSettings settings = App.ViewModelMaintenance.Setting;

    var q = from tableDeposit deposit in salesDB.Deposit
            where deposit.IsSync == false
            select deposit;

    int stat = 0;
    if (q.Count() > 0)
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri(App.ServiceURL);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            foreach (var item in q)
            {
                HttpResponseMessage depositResponse = await client.GetAsync("api/DepositSlip?DepositDate=" 
                   + item.DepositDate
                   + "&SalesDate=" + item.SalesDate
                   + "&MoneyCount=" + item.MoneyCount
                   + "&CompanyCode=" + item.DistributorCode
                   + "&UserCode=" + item.UserCode
                   + "&DeviceCode=" + item.DeviceCode
                   + "&RecordIdentity=" + item.RecordIdentity);

                if (depositResponse.IsSuccessStatusCode)
                {
                    int invStat = await depositResponse.Content.ReadAsAsync<int>();

                    if (invStat > 0)
                    {
                        tableDeposit updateDeposit = salesDB.Deposit.Single(x => x.DepositID == item.DepositID);
                        //updateDeposit.IsSync = true;
                        updateDeposit.SyncDate = DateTime.Now;
                        salesDB.SubmitChanges();
                    }
                    else
                        return 3;
                }
                else
                    return 2;
            }
        }
        stat = 1;
    }

    return stat;
}

What I want to do is to add image to this data to be uploaded.

I already have the byte array of the image. How can i upload it?

Can anyone please help me i dont know where to start.

Thank you!

1

There are 1 answers

0
Christian Amado On BEST ANSWER

If you want to upload(POST) an image why you're using client.GetAsync method (GET). You need a POST method in order to upload something to the server.

You can use MultipartFormDataContent class and HttpClient.PostAsync method.

Your code will looks something like this:

public async Task<string> UploadAllDeposit()
{
    tableSettings settings = App.ViewModelMaintenance.Setting;

    var q = from tableDeposit deposit in salesDB.Deposit
            where deposit.IsSync == false
            select deposit;

    string result  = string.Empty;
    if (q.Count() > 0)
    {
        using (var client = new HttpClient())
        {
            MultipartFormDataContent form = new MultipartFormDataContent();

            form.Add(new StringContent(token), "token");

            foreach (var item in q)
            {
                var imageForm = new ByteArrayContent(img, 0, img.Count());
                imagenForm.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");

                form.Add(imagenForm, "img", "your_image.jpg");

                HttpResponseMessage response = await client.PostAsync("URL_HERE", form);

                response.EnsureSuccessStatusCode();
            }

            client.Dispose();
            result = response.Content.ReadAsStringAsync().Result;
        }
    }

    return result;
}