使用的ASP.NET Web API,怎么能控制器返回使用DotNetZip库流图像融为一体pressed的集合? [英] Using ASP.NET Web API, how can a controller return a collection of streamed images compressed using DotNetZip Library?

查看:228
本文介绍了使用的ASP.NET Web API,怎么能控制器返回使用DotNetZip库流图像融为一体pressed的集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建一个Web API控制器,​​生成并返回从内存中的JPEG文件(MemoryStream的对象)的集合,一个COM pressed zip文件流。我试图用DotNetZip库。我发现这个例子:<一href="http://www.4guysfromrolla.com/articles/092910-1.aspx#postadlink">http://www.4guysfromrolla.com/articles/092910-1.aspx#postadlink.但Response.OutputStream没有网络可用的API,因此该技术完全不是那么回事。因此,我试图保存压缩文件到一个新的MemoryStream;但它扔了。最后,我尝试使用PushStreamContent。这是我的code:

How can I create a Web API controller that generates and returns a compressed zip file streamed from a collection of in-memory JPEG files (MemoryStream objects). I'm attempting to use DotNetZip Library. I found this example: http://www.4guysfromrolla.com/articles/092910-1.aspx#postadlink. But the Response.OutputStream is not available in Web API and so that technique doesn't quite work. Therefore I tried saving the zip file to a new MemoryStream; but it threw. Lastly, I tried using PushStreamContent. Here's my code:

    public HttpResponseMessage Get(string imageIDsList) {
        var imageIDs = imageIDsList.Split(',').Select(_ => int.Parse(_));
        var any = _dataContext.DeepZoomImages.Select(_ => _.ImageID).Where(_ => imageIDs.Contains(_)).Any();
        if (!any) {
            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
        }
        var dzImages = _dataContext.DeepZoomImages.Where(_ => imageIDs.Contains(_.ImageID));
        using (var zipFile = new ZipFile()) {
            foreach (var dzImage in dzImages) {
                var bitmap = GetFullSizeBitmap(dzImage);
                var memoryStream = new MemoryStream();
                bitmap.Save(memoryStream, ImageFormat.Jpeg);
                var fileName = string.Format("{0}.jpg", dzImage.ImageName);
                zipFile.AddEntry(fileName, memoryStream);
            }
            var response = new HttpResponseMessage(HttpStatusCode.OK);
            var memStream = new MemoryStream();
            zipFile.Save(memStream); //Null Reference Exception
            response.Content = new ByteArrayContent(memStream.ToArray());
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = string.Format("{0}_images.zip", dzImages.Count()) };
            return response;
        }
    }

zipFile.Save(memStream)抛出空引用。但无论是zip文件,也不memStream是空的,没有内部异常。所以,我不知道是什么导致了空引用。我已经使用Web API,内存流很少的经验,我从来没有使用过DotNetZipLibrary。这是一个跟进这个问题:<一href="http://stackoverflow.com/questions/14567497/want-an-efficient-asp-net-web-api-controller-that-can-reliably-return-30-to-50">Want一个有效的ASP.NET Web API控制器,​​能够可靠地回到30〜50〜3MB的JPEG

任何想法?谢谢!

推荐答案

一个更通用的方法将工作是这样的:

A more generic approach would work like this:

using Ionic.Zip; // from NUGET-Package "DotNetZip"

public HttpResponseMessage Zipped()
{
    using (var zipFile = new ZipFile())
    {
        // add all files you need from disk, database or memory
        // zipFile.AddEntry(...);

        return ZipContentResult(zipFile);
    }
}

protected HttpResponseMessage ZipContentResult(ZipFile zipFile)
{
    // inspired from http://stackoverflow.com/a/16171977/92756
    var pushStreamContent = new PushStreamContent((stream, content, context) =>
    {
        zipFile.Save(stream);
        stream.Close(); // After save we close the stream to signal that we are done writing.
    }, "application/zip");

    return new HttpResponseMessage(HttpStatusCode.OK) {Content = pushStreamContent};
}

ZipContentResult 方法也生活在一个基类,可以从任何API控制器的任何其他操作使用。

The ZipContentResult method could also live in a base class and be used from any other action in any api controller.

这篇关于使用的ASP.NET Web API,怎么能控制器返回使用DotNetZip库流图像融为一体pressed的集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆