简体   繁体   English

带有视频文件的.NET Nancy响应(自行托管)

[英].NET Nancy response with a video file (self hosting)

I am writing a self-hosting server on .NET based on REST architecture with Nancy(version 1.4.4). 我正在使用Nancy(版本1.4.4)基于REST体系结构在.NET上编写自托管服务器。 I preferred to do self-hosting(Nancy.Hosting.Self is version 1.4.1) and one of requires functionalities is to respond for a request with a video file. 我更喜欢进行自我托管(Nancy.Hosting.Self为1.4.1版),其中一项要求功能是使用视频文件响应请求。 To make picture clear, my partner writes React application and he needs this video. 为了使画面清晰,我的伙伴编写了React应用程序,他需要此视频。

I've tried different options: 我尝试了不同的选择:

First I tried Response.AsFile() but when I try to access it by a link, I get 404 with label "The resource you have requested cannot be found." 首先,我尝试使用Response.AsFile()但是当我尝试通过链接访问它时,我得到404标签为“找不到您所请求的资源”。 and I have no idea why... 我不知道为什么

public class HelloModule : NancyModule
{
    public HelloModule()
    {
        Get["/"] = parameters =>
        {
            return Response.AsFile(@"C:\7\video112018.mp4","video/mp4");
        };
    }
}

Second variant was to use GenericFileResponce like in the code below, but it leads to the same problem: 第二个变种是像下面的代码一样使用GenericFileResponce ,但这会导致相同的问题:

public class HelloModule : NancyModule
{
    public HelloModule()
    {
        Get["/"] = parameters =>
        {
            GenericFileResponse fileResponse = new GenericFileResponse(@"C:\7\video112018.mp4");
            return fileResponse;
        };
    }
}

Last option I tried was to write directly to response stream like in the code below, but in this case an error "The specified network name is no longer available" occurs. 我尝试的最后一个选择是直接写入响应流,如下面的代码所示,但是在这种情况下会出现错误“指定的网络名称不再可用”。 And what makes it tricky is that this error occurs sometimes but I didn't find any dependency where it comes from... 棘手的是,有时会发生此错误,但我没有发现它来自哪里的任何依赖关系...

public class HelloModule : NancyModule
{
    public HelloModule()
    {
        Get["/"] = parameters =>
        {
            return new Response
            {
                ContentType = "video/mp4",

                Contents = s =>
                {
                    String fileName = @"C:\7\video112018.mp4";
                    using (var stream = new FileStream(fileName, FileMode.Open))
                        stream.CopyTo(s);
                    s.Flush();
                    s.Close();
                }
            };
        };
    }
}

I would really appreciate if you have any suggestions about these solutions or give another one. 如果您对这些解决方案有任何建议或提出其他建议,我将不胜感激。

PS I also tried to send an image, it works with third approach but not with first or second PS我也尝试发送图像,它可以使用第三种方法,但不能使用第一种或第二种方法

PPS don't judge the code strictly, because it's only an example) PPS并不严格地判断代码,因为这只是示例)

I was able to do very basic streaming of video files by doing this: 通过执行以下操作,我能够进行非常基本的视频文件流传输:

Get["/"] = p =>
{
    var file = new FileStream(@"PATH_TO_FILE", FileMode.Open);
    return new StreamResponse(() => file, "video/mp4");
}

This allowed video files to play, but there was no seeking. 这样可以播放视频文件,但是没有进行搜索。

In the end I found this post. 最后我找到了这个帖子。 Adding these extensions allows for the video to be seeked. 添加这些扩展名可以搜索视频。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM