从单个 WebApi 方法提供多个二进制文件的最佳方法是什么? [英] What's the best way to serve up multiple binary files from a single WebApi method?

查看:34
本文介绍了从单个 WebApi 方法提供多个二进制文件的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ASP.NET MVC 4 Web Api 控制器方法,它传递文件 ID 列表并返回这些文件的缩略图.

I have an ASP.NET MVC 4 Web Api controller method that gets passed a list of file IDs and returns thumbnail images for those files.

因此,客户端可能会传入一个数字 ID 列表(例如 10、303、29),并且该方法返回一个列表,其中 ThumbnailImage 看起来有点像这样:

So, the client might pass in a list of numeric IDs (e.g. 10, 303, 29), and the method returns a List where a ThumbnailImage looks a bit like this:

class ThumbnailImage
{
    public int Id { get; set; }
    // Some other stuff
    public byte[] RawData { get; set; }
}

调用者传入一个 ID 列表而不是为每个项目进行一次调用的原因应该是显而易见的 - 可能有数十或数百个项目需要下载,我正在尝试避免所有可能会出现的 HTTP 流量需要单独下载.

The reason that the caller passes in a list of IDs rather than making one call per item should hopefully be obvious - there may be dozens or hundreds of items to download, and I'm trying to avoid all the HTTP traffic that would be required to download them individually.

目前,我正在使用 RestSharp 和 JSON.NET,因此我的 ThumbnailImage 对象作为 JSON 通过网络传递.从简单编码的角度来看这很好,但 JSON 并不是表示二进制数据的有效方式.

Currently, I'm using RestSharp and JSON.NET, and so my ThumbnailImage objects are being passed across the wire as JSON. It's fine from a simplicity-of-coding point of view, but JSON is not an efficient way to represent that binary data.

所以,我想我应该将原始字节作为八位字节流返回......但是,虽然我可以轻松地对单个图像执行此操作,但我不确定这样做的最佳方法多个图像,尤其是当我还需要返回每个文件的 ID 和其他杂项信息时.(ID 是必需的,因为结果不一定会按给定的顺序返回 - 并且某些文件可能会丢失).

So, I'm thinking that I should return the raw bytes as an octet-stream... however, while I can easily do that for a single image, I'm not sure of the best way to do it for multiple images, especially when I also need to return the ID and miscellaneous other information for each file. (The ID is required since the results will not necessarily be returned in a given order - and some files may be missing).

可以简单地将所有内容零碎地写入响应流中,以便为每个项目写入 ID(适当编码),然后是图像数据的长度,然后是图像数据本身,然后是下一个项目的相同内容,等等.

I could simply write everything piecemeal into the response stream, so that for each item I write the ID (suitably encoded), followed by the length of the image data, followed by the image data itself, and then followed by the same thing for the next item, etc.

然后调用者会简单地继续从流中读取,直到用完为止,并对 ID 的编码(和长度!)等做出假设.

The caller would then simply keep reading from the stream until it was exhausted, making assumptions about the encoding (and length!) of the IDs, etc.

我认为这可行,但看起来很笨重 - 有没有更好的方法?

I think that would work, but it seems clunky - is there a better way?

推荐答案

好的,这里的代码片段似乎可以工作,使用 KiranChalla 提到的 MultipartContent.(这只是一个虚拟示例,展示了如何结合 JSON 编码的对象"(在本例中只是整数 ID 列表)返回两个不同类型的文件.

OK, here's a snippet of code that seems to work, using the MultipartContent that KiranChalla referred to. (This is just a dummy sample that shows how to return two files of different types, in conjunction with a JSON-encoded "object" (which in this case is just a list of integer IDs).

public HttpResponseMessage Get()
{
    var content = new MultipartContent();
    var ids = new List<int>() { 1, 2 };

    var objectContent = new ObjectContent<List<int>>(ids, new System.Net.Http.Formatting.JsonMediaTypeFormatter());
    content.Add(objectContent);

    var file1Content = new StreamContent(new FileStream(@"c:	empdesert.jpg", FileMode.Open));
    file1Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("image/jpeg");
    content.Add(file1Content);

    var file2Content = new StreamContent(new FileStream(@"c:	emp	est.txt", FileMode.Open));
    file2Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("text/plain");
    content.Add(file2Content);

    var response = new HttpResponseMessage();
    response.Content = content;
    return response;
}

这篇关于从单个 WebApi 方法提供多个二进制文件的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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