简体   繁体   中英

Am getting Bad Request while uploading an Image to the server (backend) , am using xam.plugin.media

Am trying to upload an image with the help of Xam.plugin.media NuGet package. But am getting a 400 Bad Request .

Below is my method which uploads the image to the backend.

private async void UploadImage()
        {
            var token = Application.Current.Properties["token"].ToString();


            var multiForm = new MultipartFormDataContent();


            var imagFile = _mediaFile.AlbumPath;
            var upfilebytes = File.ReadAllBytes(imagFile);
            ByteArrayContent baContent = new ByteArrayContent(upfilebytes);

            multiForm.Add(new StringContent("1"), "x");
            multiForm.Add(new StringContent("100"), "y");
            multiForm.Add(new StringContent("100"), "width");
            multiForm.Add(new StringContent("100"), "height");


            multiForm.Add(baContent, "file", Path.GetFileName(_mediaFile.AlbumPath));

            var client = new HttpClient();
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "eyJhbGciOiJIUzI1NiIsInR5cCI6" +
                "IkpXVCJ9.eyJYwZi05MjhhLTRiZjctYjZkNi0wY2U1ODRkOGRjZmQiLCJ1bmlxdWVfbmFtZ" +
                "SI6IjIwZDA0MDY3LTU3YzYtNGY2MC04ZDI5LTg1NzkxMmFmOWI2MSIsImNsdCI6ImVuLUdCIiwidHoiOiJ" +
                "XLiBFdXJvcGUgU3RhbmRhtZSIsIm5iZiI6MTU1NjQ3NTMyNCwiZXhwIjoxNTU2NDgyNTI0LCJpY" +
                "XQiOjE1NTY0NzUzMjQsImlzcyI6Imh0dHBzOi8vYXBpLnZpYS5zb2Z0d2FyZSIsImF1ZCI6Imh0dHBz" +
                "Oi8vd3d3LnZpYS5zb2Z0d2FyZSJ9.z5rghA338FDAtoInFYugMgwIirrdl9CrsGKhS8ceoFI");
            var response = await client.PostAsync(utils.Settings.BaseUrl + "/auth/Users/ChangeAvatar", multiForm);

            Debug.WriteLine("--> * "+response.Content.ReadAsStreamAsync().Result);

            Debug.WriteLine("response --> * "+_mediaFile.AlbumPath);
            Debug.WriteLine("--> * " + response);
        }

So according to what I have implemented, I thought everything could work out.

Below are the parameters needed by the backend, actually when I try testing the endpoint in postman it works fine, but the method above doesn't work.

在此处输入图片说明

This is what happens on postman everything works fine when I upload an image.

在此处输入图片说明

What could be missing in my code for uploading?

服务需要一个名为“ image”的参数,但是您要传递一个名为“ file”的参数

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM