WCF 数据服务可用于返回文件吗?
我想使用 WCF 数据服务返回 mp3 文件以及 WCF 已提供的其他数据类型。 这可能吗?
[12/29] 我确实有 sql 中的 mp3 二进制数据(每个大约 10 秒)。我想使用 html 音频标签:
<音频 src="/server/svc/id" type="audio/mpeg" />
JavaScript 会改变 id。不确定这将如何与返回二进制数组的 WCF 一起工作...
[12/30] 它可以工作.... 在 CSDL 中设置 mimetype 后。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然,您可以通过三种可能的方式来做到这一点:
将其作为现有实体的二进制属性包含在内(在这种情况下,通常服务托管代表每个音频文件的实体,其属性之一是实际的音频 mp3 )。在代码中,这只是声明一个 byte[] 类型的属性(并填充它)。好处是这非常容易设置。缺点是,当您 GET 这样的实体时,整个 mp3 都会被下载,并以 BASE64 编码文本的形式传输,因此传输量很大。
与上面的设置类似,但不是将 mp3 存储在二进制属性中,而是将实体设为媒体资源条目,而是使其具有默认流值。此博客文章对此进行了更详细的描述:http://blogs.msdn.com/b/astoriateam/archive/2010/08/04/data-services-streaming-provider-series-implementing-a-streaming-provider-part-1。 aspx 和 http://blogs.msdn.com/b/astoriateam/archive/2010/09/08/data-services-streaming-provider-series-part-2-accessing-a-media-resource- Stream-from-the-client.aspx。
在最新的CTP WCF DS中也支持流属性。这与上面的 #2 类似,但单个实体可以有多个流,每个流都可以通过其名称进行访问。
要访问音频流的原始值,您可以使用 URL(取决于您用于公开流的方法):
urltotheentity/MyBinaryProperty/$value
urltotheentity/ $value
urltotheentity/StreamPropertyName/$value
请注意,在 #1 中,流的默认 MIME 类型将为 application/octet-stream,要更改它,您需要为属性(反射提供程序)或属性添加一个属性 MimeType 到 CSDL 中属性(EF 提供者)。
在#2 和#3 中,mime 类型是通过调用流提供者来确定的。
Sure, you can do this in three possible ways:
Include it as a binary property on an existing entity (usually the service hosts entities representing each audio file in this case, which would have one of its properties being the actual audio mp3). In code this is just to declare a property of type byte[] (And fill it in). The upside is that this is really easy to setup. The downside is that when you GET such entity the entire mp3 gets downloaded and it's transferred as BASE64 encoded text, so it's big on the wire.
Similar setup as above, but instead of storing the mp3 in a binary property you make the entity a Media Resource Entry, you make it have a default stream value. This is described in this blog post in more detail: http://blogs.msdn.com/b/astoriateam/archive/2010/08/04/data-services-streaming-provider-series-implementing-a-streaming-provider-part-1.aspx and http://blogs.msdn.com/b/astoriateam/archive/2010/09/08/data-services-streaming-provider-series-part-2-accessing-a-media-resource-stream-from-the-client.aspx.
In the latest CTP WCF DS also supports stream properties. Which is similar to #2 above but a single entity can have multiple streams each accessible by its name.
To access the raw value of the audio stream you can use URL (depends on the method you use to expose the stream):
urltotheentity/MyBinaryProperty/$value
urltotheentity/$value
urltotheentity/StreamPropertyName/$value
Note that in #1 the default MIME type of the stream will be application/octet-stream, to change it you need to add an attribute MimeType for the property (reflection provider) or attribute into the CSDL for that property (EF provider).
In #2 and #3 the mime type is determined via a call to the stream provider.