我有基于HttpListener的小型本地web服务器。服务器向本地客户端应用程序提供文件,解包和写入文件
response.OutputStream;但有时文件(视频)很大,我不认为总是将所有文件字节复制到输出流(内存)中是个好主意。我想将服务文件流连接到响应输出流,如下所示:
response.OutputStream = myFileStream;但是- -ok- response.OutputStream是只读的,所以我只能写字节--有没有办法进行某种部分写入(流)?
致以问候。
发布于 2013-11-09 05:24:43
您需要创建一个线程并将您的数据流式传输到response。使用类似如下的内容:
在你的主线程中:
while (Listening)
{
// wait for next incoming request
var result = listener.BeginGetContext(ListenerCallback, listener);
result.AsyncWaitHandle.WaitOne();
}在你班上的某个地方:
public static void ListenerCallback(IAsyncResult result)
{
var listenerClosure = (HttpListener)result.AsyncState;
var contextClosure = listenerClosure.EndGetContext(result);
// do not process request on the dispatcher thread, schedule it on ThreadPool
// otherwise you will prevent other incoming requests from being dispatched
ThreadPool.QueueUserWorkItem(
ctx =>
{
var response = (HttpListenerResponse)ctx;
using (var stream = ... )
{
stream.CopyTo(response.ResponseStream);
}
response.Close();
}, contextClosure.Response);
}https://stackoverflow.com/questions/18011788
复制相似问题