Quickblox Upload file rest api call in c# giving 400

Question!

I am using api to upload profile image... Reference link :http://quickblox.com/developers/Content#API_Content_Upload_File

var uri = new Uri(getparam);
                                var query = HttpUtility.ParseQueryString(uri.Query);
                                var ContentType = query.Get("Content-Type");
                                var Expires = query.Get("Expires");
                                var acl = query.Get("acl");
                                var key = query.Get("key");
                                var policy = query.Get("policy");
                                var successactionstatus = query.Get("success_action_status");
                                var xamzalgorithm = query.Get("x-amz-algorithm");
                                var xamzcredential = query.Get("x-amz-credential");
                                var xamzdate = query.Get("x-amz-date");
                                var xamzsignature = query.Get("x-amz-signature");                              

                                string profileimagepath = Path.Combine(HttpContext.Current.Server.MapPath("~/Content/ProfileImage/"));
                                var fullpath=profileimagepath + newFileName;
                                var fileName = System.IO.Path.GetFileName(Convert.ToString(fullpath));


                                using (var wb = new WebClient())
                                {

                                    var data = new NameValueCollection();
                                    data["Content-Type"] = ContentType;
                                    data["Expires"] = Expires;
                                    data["acl"] = acl;
                                    data["key"] = key;
                                    data["policy"] = policy;
                                    data["success_action_status"] = successactionstatus;`enter code here`
                                    data["x-amz-algorithm"] = xamzalgorithm;
                                    data["x-amz-credential"] = xamzcredential;
                                    data["x-amz-date"] = xamzdate;
                                    data["x-amz-signature"] = xamzsignature;
                                    //data["file"] = ImageToBase64(fullpath);
                                    data["file"] = @filename;
                                     wb.UploadFile("https://qbprod.s3.amazonaws.com/", "POST", fullpath);

                                    var qbUploadFileResponse = wb.UploadValues("https://qbprod.s3.amazonaws.com/", data);

                                }

Its giving me 400 Bad request in wb.UploadValues().. It is working perfect in rest client(Postman).

Please help



Answers
Instead of using web client you should use http client and send as MultipartFormDataContent. It worked for me!!!!

HttpContent bytesContent = new ByteArrayContent(ImageToBase64(fullpath));
                                using (var client = new HttpClient())
                                using (var formData = new MultipartFormDataContent())
                                {

                                    formData.Add(new StringContent(ContentType), "Content-Type");
                                    formData.Add(new StringContent(Expires), "Expires");
                                    formData.Add(new StringContent(acl), "acl");
                                    formData.Add(new StringContent(key), "key");
                                    formData.Add(new StringContent(policy), "policy");
                                    formData.Add(new StringContent(successactionstatus), "success_action_status");
                                    formData.Add(new StringContent(xamzalgorithm), "x-amz-algorithm");
                                    formData.Add(new StringContent(xamzcredential), "x-amz-credential");
                                    formData.Add(new StringContent(xamzdate), "x-amz-date");
                                    formData.Add(new StringContent(xamzsignature), "x-amz-signature");
                                    formData.Add(bytesContent, "file", fileName);
                                    var responseTemp = client.PostAsync("https://qbprod.s3.amazonaws.com/", formData).Result;
                                    if (!responseTemp.IsSuccessStatusCode)
                                    {
                                        resultResponse.ErrorCode = "0";
                                        resultResponse.Message = "Profile Save Succesfully. But Quick Bolx Blob A/c Not Updated";
                                        resultResponse.Data = response.Response;
                                        return Ok(resultResponse);
                                    }
                                    var respon = responseTemp.Content.ReadAsStreamAsync().Result;

                                    if (respon != null)
                                    {
                                        var finalResponse = DeclaringFileUpload(gettokenbyuser, Quickbolxblob_id);


////// convert image in Base64 byte.
 public byte[] ImageToBase64(string path)
        {
            using (Image image = Image.FromFile(path))
            {
                using (MemoryStream m = new MemoryStream())
                {
                    image.Save(m, image.RawFormat);
                    byte[] imageBytes = m.ToArray();

                    // Convert byte[] to Base64 String
                    string base64String = Convert.ToBase64String(imageBytes);
                    return imageBytes;
                }
            }
        }


This video can help you solving your question :)
By: admin