简体   繁体   English

Xamarin将照片发布到Azure移动服务

[英]Xamarin post photo to azure mobile service

For some reason when i post from my xamarin app i get a 500, using chrome Advanced Rest Client i can post a photo so the service is working (the problem is in the app post)... 由于某些原因,当我从我的xamarin应用程序发布时,我得到了500,使用chrome Advanced Rest Client,我可以发布照片,以便该服务正常工作(问题出在应用程序发布中)...

My Controller: 我的控制器:

   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; }
}

My post: 我的帖子:

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;
    }

response statuscode: Internal server error 响应状态码:内部服务器错误

Trying this as a solution.... added Content-type and Content-Disposition 尝试将其作为解决方案。...添加了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;
}

since you are getting a 500 error message, my suggestion is to first look at the Log from the Azure Portal Dashboard. 由于收到500错误消息,因此我的建议是首先查看Azure Portal仪表板中的日志。 It may pinpoint exactly the error, probably an attribute mismatch.. 它可能会精确指出错误,可能是属性不匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM