Quickblox在C#中上传文件Rest API调用400

迈希德·曼德维瓦拉

我正在使用api上传个人资料图片...参考链接: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);

                                }

它在wb.UploadValues()中给了我400错误的请求。

请帮忙

希瓦姆·阿加瓦尔(Shivam Agarwal)

而不是使用Web客户端,您应该使用http客户端并以MultipartFormDataContent的形式发送。它为我工作!!

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;
                }
            }
        }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Microsoft Graph API Rest调用在c#中上传超过4MB

在同一API调用C#中上传文件和数据

Rest扩展API以在marklogic中上传文件

在Spring Boot REST API中上传文件

C# Rest API 文件上传

在ColdFusion 10的REST API框架中上传文件

Quickblox:上传图片REST API

Get Token 调用 Microsoft Graph REST Api 给出 400 错误

解决 Rest API 调用状态码 400 问题

magento 2 REST API调用期间出现400错误请求

将文件上传到WindowsForms C#中的Rest API

如何使用Rest API和Java在Egnyte中上传pdf文件

在Angular 8应用中上传媒体文件时,如何从rest api返回响应?

Docusign上传api调用返回远程服务器返回错误:(400)错误的请求

Unity 在 SAP Rest API 上调用 HTTP-Post 时发生 400 Bad Request

为什么我得到400状态代码来进行Rest API删除调用

使用Spring Oauth2的Rest Api调用给出错误400

在Jersey REST Service中调用Google Geocode API会导致内部400错误

可以从Postman调用Rest API,显示来自Java的400错误请求

使用C#中的添加功能使用转义字符的Sharepoint REST api上传文件

as / 400:从CL调用C过程

Azure 自动化在混合工作器上调用内部应用程序 Rest API 返回 400 错误

使用REST API和C#将PDF文件上传到Azure Blob存储(不使用任何AZURE STORAGE SDK)

Azure - C# - 使用 Rest API 上传图像时出错

OneDrive REST API提供了400个无效动词用于文件上传

使用Web API在Ionic应用程序中上传文件

使用Google Drive API在指定的Google Drive中上传文件

如何使用 Python 在 Autentique API 中上传文件?

YouTube API和C#错误400?