如何在 xamarin 表单中使用 HttpClient post 发送文件

How to send file with HttpClient post in xamarin forms

我想使用 httpclient 通过 post 请求发送文件 这是我尝试过但文件没有发送的方法,当我在 postman 中尝试时它工作正常

string Url = $"http://ataprojects.net/test/products.php?request_type=add&company_name={BaseService.Company}&name={product.name}&barcode={product.barcode}&buy_price={product.buy_price}&sell_price={product.sell_price}";
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    var content = new MultipartFormDataContent();
                    content.Headers.ContentType.MediaType = "multipart/form-data";
                    content.Add(new StreamContent(product._mediaFile.GetStream()),
                      "image",
                       product.image);                    
                    var response = client.PostAsync(Url, content).Result;

                    response.EnsureSuccessStatusCode();
                    if (response.IsSuccessStatusCode)
                    {
                        var contentdata = await response.Content.ReadAsStringAsync();                       
                        var Items = JsonConvert.DeserializeObject<AddProductReturnModel>(contentdata);
                        return Items;
                    }
                    else
                    {
                        return null;
                    }
                }

            }

有什么问题吗?

试试这个代码

 var content = new MultipartFormDataContent();
                    content.Add(new StreamContent(product._mediaFile.GetStream()),
                        "\"file\"",
                        $"\"{product._mediaFile.Path}\"");