【问题标题】:send image to rest service xamarin forms将图像发送到休息服务 xamarin 表单
【发布时间】:2018-07-08 06:47:01
【问题描述】:

我正在尝试将图像从 Xamarin Forms 发送到 Rest WebApi,但没有成功。我正在使用 Montemagno 的 CrossMedia 插件。我以这种方式将 MediaFile 转换为 base64String:

if (photo != null)
{
    var stream = photo.GetStream();
    var bytes = new byte[stream.Length];
    await stream.ReadAsync(bytes, 0, (int)stream.Length);
    string imageBase64 = Convert.ToBase64String(bytes);
    Task<string> sendFotoResult = restClient.SendImage(imageBase64);
    string result = await sendFotoResult;
    if( ... )
}

这是我的 SendImage 函数:

public async Task<string> SendImage(string foto)
{
  try
  {
     // METHOD 1 
     var content = JsonConvert.SerializeObject(foto);
     string url = "http://myaddress/myWS/api/Home/SendImage?foto="+ content;
     var response = await _client.PostAsync(url, new StringContent(content, Encoding.UTF8, "application/json"));
     return response.ReasonPhrase.ToString();

     //METHOD 2
     var content = JsonConvert.SerializeObject(foto);
     string url = "http://myaddress/myWS/api/Home/SendImage?foto="+ content;
     var result = await _client.PostAsync(url, new StringContent(content, Encoding.UTF8, "application/json"));
     return result.ToString(); 
  }catch (Exception ex)
  {
    return ex.Message;
  }
}

方法1显示空参数错误,方法2获取URL太长错误。

我该如何解决? 将图像转换为 base64String 是发送它的最佳方式吗?

非常感谢。

【问题讨论】:

  • 在查询字符串中发送图像是一个非常糟糕的主意。
  • 这是我第一次,我想有很多更好的方法可以做到这一点,我只是问他们。

标签: c# image rest web-services xamarin.forms


【解决方案1】:

您不应该在 URI 上发送图像。您所要做的就是在您的请求正文中发送图像。这样的事情会帮助你:

var client = new HttpClient();
var form = new MultipartFormDataContent();
form.Add(new ByteArrayContent(new MemoryStream(foto).ToArray()), "foto", "foto.jpg");

然后你必须在你的服务器端 api 上管理图像并将其转换回来。

编辑:我假设如果您还控制您的 REST api,那么您必须尝试从 uri 获取图像。您不应该这样做,而是您必须从内容中获取它。这里有一个教程将引导你一路走好:https://jamessdixon.wordpress.com/2013/10/01/handling-images-in-webapi/

另外,您应该调用不带参数的 POST 方法,如下所示:

string url = "http://myaddress/myWS/api/Home/"

如果你已经在 Home 控制器上管理 POST 来做一些其他的工作,那么你总是可以使用路由并调用这样的东西:

string url = "http://myaddress/myWS/api/Home/Images/"

ASP.NET 上的路由管理非常简单,并且在此链接上进行了很好的描述:https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

最后,我怀疑您可能需要加强您对标准所关注的 REST 的了解。我总是推荐以下页面来了解设计 RESTful API 应该和不应该做什么:http://blog.octo.com/en/design-a-rest-api/

【讨论】:

  • 也许使用 MultipartFormDataContent() 是正确的方法。如何正确编写网址?
  • 当然,我已经强化了我的 REST 知识,你是对的。这是我的第一个方法,这条路还很漫长。在此之前,您建议我如何解决?
  • 虽然你消失了你的链接是非常有用的。我解决了。谢谢。
【解决方案2】:

您可以使用 MultipartFormDataContent 将图像部分添加到 post 请求中,试试这个例子

            var upfilebytes = DependencyService.Get<ILocalFileProvider>().GetFileBytes(FileUrl);
            MultipartFormDataContent content = new MultipartFormDataContent();
            ByteArrayContent baContent = new ByteArrayContent(upfilebytes);
            content.Add(baContent, "File", "attachment.png");
            var response = await client.PostAsync(url, content);

【讨论】:

    猜你喜欢
    • 2012-10-11
    • 1970-01-01
    • 2013-11-05
    • 2015-12-22
    • 1970-01-01
    • 2011-07-01
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多