将图像上传到azure blob的最佳做法

时间:2014-02-05 20:31:58

标签: c# azure asp.net-web-api azure-storage-blobs

我正在使用xamarin构建移动应用程序,而服务器托管在azure中。我按以下方式上传图片: 客户端:

public static async Task<string> UploadImage (string url, byte[] imageData)
        {
            var content = new MultipartFormDataContent();
            var fileContent = new ByteArrayContent(imageData);
            fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = Guid.NewGuid() + ".Png"
            };
            content.Add(fileContent);

            using (var client = new HttpClient())
            {
                try
                {
                    HttpResponseMessage msg = await client.PutAsync (url, content);
                    if(msg.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        return msg.Headers.GetValues ("ImageUrl").First();
                    }
                    return string.Empty;

                }
                catch (Exception ex)
                {
                    return string.Empty;
                }
            }          
        }

这是服务器代码:

[HttpPut]
        public async Task<HttpResponseMessage> PostNewDishImage(string imageID)
        {
            try
            {
                _dishImagescontainer = BlobStorageHandler.GetContainer("dishuserimages");
                if (!Request.Content.IsMimeMultipartContent("form-data"))
                {
                    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType));
                }

                var provider = new BlobStorageProvider(_dishImagescontainer);
                await Request.Content.ReadAsMultipartAsync(provider);
                IList<string> urls = provider.Urls;
                if (urls.Count > 0)
                {
                    var response = new HttpResponseMessage();
                    response.StatusCode = HttpStatusCode.OK;
                    response.Headers.Add("ImageUrl", urls[0]);
                    return response;
                }
                return new HttpResponseMessage(HttpStatusCode.InternalServerError);
            }
            catch (System.Exception e)
            {
                return new HttpResponseMessage(HttpStatusCode.InternalServerError) { ReasonPhrase = e.ToString() };
            }
        }

它工作正常,但我不喜欢我将新imageurl返回给客户端的方式(通过http标头)我尝试了一些其他方法,但这是迄今为止最好的:) 有没有人有更好的想法?

由于

1 个答案:

答案 0 :(得分:0)

在HTTP标头中将数据返回给客户端,就像有一点气味。如何将DTO序列化为JSON?

示例DTO类:

public class ImageUploadResponse
{
    public string Url { get; set; }
}

然后将服务器端代码更改为以下内容:

var responseDto = new ImageUploadResponse { Url = urls[0] };
var response = new HttpResponseMessage(HttpStatusCode.OK)
    {
       Content = new StringContent(JsonConvert.SerializeObject(responseDto), 
           Encoding.UTF8, "application/json")
    };

它会将结果作为JSON发送回HTTP响应内容中的客户端。然后,客户端可以根据需要将JSON解析为对象(或不)。如果您愿意,这种方法对于将来从JavaScript进行此类调用也会更友好,因为结果是标准JSON。