【问题标题】:Xamarin post photo to azure mobile serviceXamarin 将照片发布到 azure 移动服务
【发布时间】:2015-07-10 22:00:31
【问题描述】:

由于某种原因,当我从我的 xamarin 应用程序发布时,我得到了 500,使用 chrome Advanced Rest Client 我可以发布照片,以便服务正常工作(问题出在应用程序帖子中)...

我的控制器:

   public class BlobController : ApiController
    {
        public Task<List<FileDetails>> Post()
        {
            if (!Request.Content.IsMimeMultipartContent("form-data"))
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            return FileStorage.StoreFiles(WebConfigurationManager.AppSettings["WebApiContainer"], Request);
        }

public class FileDetails
{
    public string Name { get; set; }
    public long Size { get; set; }
    public string ContentType { get; set; }
    public string Location { get; set; }
}

我的帖子:

public async Task<FileDetails> PostPhotoRequest(string fileName, MemoryStream file)
{
    byte[] response = await _service.PostFileRequest("Blob", fileName, file);
    string json = System.Text.Encoding.UTF8.GetString(response, 0, response.Length);
    if (string.IsNullOrEmpty(json)) { return new FileDetails(); }
    return JsonConvert.DeserializeObject<FileDetails>(json);
}


    public async Task<byte[]> PostFileRequest(string url, string filename, MemoryStream file)
    {
        var client = new HttpClient();
        var content = new MultipartFormDataContent("boundary=----WebKitFormBoundaryGQ9UK82gG8XpzEBT");
        content.Add(new StreamContent(file));
        content.Add(new FormUrlEncodedContent(new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("filename", filename) }));
        content.Headers.Add("X-ZUMO-APPLICATION", Constants.MobileServiceApplicationKey);
        var response = await client.PostAsync(Constants.MobileServiceUrl + "api/" + url, content);
        if (response.StatusCode == HttpStatusCode.OK)
        {
            byte[] responseContent = await response.Content.ReadAsByteArrayAsync();
            return responseContent;
        }

        return null;
    }

响应状态码:内部服务器错误

【问题讨论】:

    标签: c# http post xamarin photo


    【解决方案1】:

    由于您收到 500 错误消息, 我的建议是首先查看 Azure 门户仪表板中的日志。 它可能会准确指出错误,可能是属性不匹配..

    【讨论】:

      【解决方案2】:

      尝试将此作为解决方案....添加了 Content-type 和 Content-Disposition

      public async Task<byte[]> PostFileRequest(string url, string filename, MemoryStream file)
      {
          var client = new HttpClient();
          var content = new MultipartFormDataContent();
          var streamContent = new StreamContent(file);
          streamContent.Headers.Add("Content-Type", "application/octet-stream");
          streamContent.Headers.Add("Content-Disposition", "form-data; name=\"file\"; filename=\"" + filename + "\"");
          content.Add(streamContent, "file", filename);
          content.Headers.Add("X-ZUMO-APPLICATION", Constants.MobileServiceApplicationKey);
          var response = await client.PostAsync(Constants.MobileServiceUrl + "api/" + url, content);
          if (response.StatusCode == HttpStatusCode.OK)
          {
              byte[] responseContent = await response.Content.ReadAsByteArrayAsync();
              return responseContent;
          }
      
          return null;
      }
      

      【讨论】:

        猜你喜欢
        • 2013-06-25
        • 2018-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多