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

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

问题描述

我有被传递文件ID的列表并返回缩略图这些文件的ASP.NET MVC 4的Web API控制器的方法。

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是不是要重新present一种有效的方式,二进制数据。

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(适当烯codeD),接着将图像数据的长度,然后的图像数据本身,然后接着进行下一个项目,等等同样的事情。

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?

推荐答案

好吧,这里似乎工作,使用MultipartContent的KiranChalla简称code片段。 (这只是一个虚拟的示例,演示如何使用JSON-CN codeD对象(在这种情况下,仅仅是一个整数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:\temp\desert.jpg", FileMode.Open));

    file1Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("image/jpeg");

    content.Add(file1Content);


    var file2Content = new StreamContent(new FileStream(@"c:\temp\test.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天全站免登陆