0

如果我通过 Insomnia 将图像上传到我的 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 "";
            }
        } 
4

1 回答 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();
于 2020-03-20T01:28:17.580 回答