【问题标题】:Xamarin Forms upload image Laravel POST Request failedXamarin Forms 上传图片 Laravel POST 请求失败
【发布时间】:2020-03-04 05:42:38
【问题描述】:

如果我通过 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 "";
            }
        } 

【问题讨论】:

  • 确保你使用的流是正确的,这里是上传图片到服务器端的an example,注意content.Add...这一行。
  • 谢谢它的工作:)
  • 能否请您标记答案,以便我们可以帮助更多有相同问题的人?

标签: android xamarin http-post xamarin-forms-4


【解决方案1】:

在 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();

【讨论】:

    猜你喜欢
    • 2013-03-21
    • 2019-06-30
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 2012-03-07
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多