Want an efficient ASP.NET Web API controller that

2019-08-24 01:51发布

I'd like to change the following code into an efficient Web API controller method that can return 30 to 50 JPEGs (for <20 simultaneous users) of average 3MB size.

    public async Task<HttpResponseMessage> Get(int imageid) {
        return await Task.Run(() => {
            var dzImage = _dataContext.DeepZoomImages.SingleOrDefault(_ => _.ImageID == imageid);
            if (dzImage == default(DeepZoomImage)) {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
            }
            var response = new HttpResponseMessage(HttpStatusCode.OK);
            var bitmap = GetFullSizeBitmap(dzImage);
            var memoryStream = new MemoryStream();
            bitmap.Save(memoryStream, ImageFormat.Jpeg);
            response.Content = new ByteArrayContent(memoryStream.ToArray());
            var fileName = string.Format("{0}.jpg", dzImage.ImageName);
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = fileName };
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
            memoryStream.Flush();
            return response;
        });
    }

The foregoing method handles one image. How can I best pluralize it to handle multiple images efficiently for this usage/load scenario? Is it possible to return Task<HttpResponseMessage>[] or Task<HttpResponseMessage[]>?

Or would it be better generate all images and return them in one go?

Thanks for your help...

1条回答
Animai°情兽
2楼-- · 2019-08-24 02:20

Based on our discussion the best approach here would be to create a ZIP file on the server and then download that one file to the user. As an alternative you could try this multi-file download approach.

查看更多
登录 后发表回答