[英]How to send a file to clients using owin httplistener?
I've googled a lot about sending a file from a console application using an own HTTP listener self-hosted to a web application that hosted on ASP.NET MVC5 I've found a method in response named res.SendFileAsync
, but I don't know how to use it. 我在Google上搜索了大量有关使用自己的HTTP侦听器从控制台应用程序向ASP.NET MVC5托管的Web应用程序发送文件的信息,我找到了一个名为
res.SendFileAsync
响应方法,但是我没有不知道如何使用它。 Here's my code: 这是我的代码:
public void Configuration(IAppBuilder app)
{
app.UseHandlerAsync((req, res) =>
{
Console.Clear();
foreach (var item in req.Headers)
{
Console.WriteLine(item.Key + ":" );
foreach (var item1 in item.Value)
{
Console.Write(item1);
}
}
//res.Headers.AcceptRanges.Add("bytes");
//result.StatusCode = HttpStatusCode.OK;
//result.Content = new StreamContent(st);
//result.Content.Headers.ContentLength = st.Length;
res.Headers.Add("ContentType" ,new string[]{"application/octet-stream"});
res.SendFileAsync(Directory.GetCurrentDirectory() + @"\1.mp3");
// res.ContentType = "text/plain";
return res.WriteAsync("Hello, World!");
});
}
This is an own startup class that handles HTTP requests. 这是处理HTTP请求的自己的启动类。
There is a full tutorial here which will cover all of your needs, basically to save you time, if you want to send a byte[]
to all your clients (which is how you should send a file) it should look like this: 有一个完整的教程这里将涵盖所有的需求,基本形成节约您的时间,如果你想发送一个
byte[]
到所有的客户端(这是你应该如何发送文件),它应该是这样的:
static void Main(string[] args) {
//---listen at the specified IP and port no.---
Console.WriteLine("Listening...");
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(IPAddress.Any, PORT_NO));
serverSocket.Listen(4); //the maximum pending client, define as you wish
serverSocket.BeginAccept(new AsyncCallback(acceptCallback), null);
//normally, there isn't anything else needed here
string result = "";
do {
result = Console.ReadLine();
if (result.ToLower().Trim() != "exit") {
byte[] bytes = null;
//you can use `result` and change it to `bytes` by any mechanism which you want
//the mechanism which suits you is probably the hex string to byte[]
//this is the reason why you may want to list the client sockets
foreach(Socket socket in clientSockets)
socket.Send(bytes); //send everything to all clients as bytes
}
} while (result.ToLower().Trim() != "exit");
}
I'd recommend you to delve deeper into the post and understand the entire process, see if it fits your solution. 我建议您深入研究该帖子并了解整个过程,看看它是否适合您的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.