如何在不缓冲的情况下将文件从数据库异步传输到 Web 客户端 [英] How to async stream a file from database to webclient without buffering
问题描述
我正在尝试使用 .net 4.5 和 web api 将文件从 sql server 异步流式传输到 web 客户端.
I'm trying to async stream a file from sql server out to a web client using .net 4.5 and web api.
我正在使用 SqlDataReader.GetStream() 从数据库获取流.但是,当 webapi 完成从流中读取时,我不确定如何处理/关闭 db 连接.
I'm using SqlDataReader.GetStream() to get a stream from the db. However, I'm not sure how to wire in the disposal/closing of the db connection when webapi is finished reading from the stream.
有样品吗?
推荐答案
您可以编写一个包装流,该流从底层流读取,直到耗尽,然后处理所有相关资源(如 SqlConnection).
You can write a wrapping stream that reads from the underlying stream until it is depleted, and then disposes of all associated resources (like the SqlConnection).
我不是 WebAPI 专家,所以可能有更优雅的方法来做到这一点.但这在任何情况下都适用于对流的标准 WebAPI 支持.
I'm not a WebAPI expert so there might be a more elegant way to do it. But this will work in any case with the standard WebAPI support for streams.
这篇关于如何在不缓冲的情况下将文件从数据库异步传输到 Web 客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!