Xamarin Forms上传图像Laravel POST请求失败

问题描述 投票:0回答:1

如果我通过失眠将图像上传到我的Laravel应用程序,一切正常,并且得到了以下日志

[2020-03-04 05:26:39] local.DEBUG: array (
  'image' => 
  Illuminate\Http\UploadedFile::__set_state(array(
     'test' => false,
     'originalName' => 'Screenshot_1583210368.png',
     'mimeType' => 'image/png',
     'error' => 0,
     'hashName' => NULL,
  )),
)  

但是如果我在android模拟器上上传图像,Laravel中的日志文件看起来像(1000多行):

�"@�G����ޠ��U��H��I�G"Ĉ`.SB^G

这是我的Xamarin函数:

public async Task<string> uploadImage(Stream stream)
        {
            using (var client = new HttpClient())
            {


                var content = new MultipartFormDataContent();


                content.Add(new StreamContent(stream),"image");


                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", (Application.Current.Properties["access_token"].ToString()));


                var result = await client.PostAsync("https://example.com/api/upload/image", content);

                return "";
            }
        } 
android xamarin http-post xamarin-forms-4
1个回答
0
投票

在Xamarin中将图像上传到服务器端的示例:

private MediaFile _image;

// code here to assign image to _image

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

var httpClient = new System.Net.Http.HttpClient();
var url = "http://upload.here.io/folder/subdir";
var responseMsg = await httpClient.PostAsync(url, content);

var remotePath =  await responseMsg.Content.ReadAsStringAsync();
© www.soinside.com 2019 - 2024. All rights reserved.