简体   繁体   English

ASP.NET MVC流文本到src标记

[英]ASP.NET MVC stream text to src tag

I'm trying to figure out a good pattern to accomplish the following: 我正在尝试找出一个好的模式来完成以下任务:

<video width="640" height="480" controls>
    <source src="@Model.MeetingVideo" type="video/mp4">
    <track kind="subtitles" label="English subtitles" src="@Model.Subtitles" srclang="en" default/>
</video>

I want to be able to store the src text in my ViewModel ( @Model.Subtitles ) and have it displayed. 我希望能够将src文本存储在ViewModel( @Model.Subtitles )中并显示它。

It is just text content. 它只是文本内容。 Normally, these subtitle files are static text files. 通常,这些字幕文件是静态文本文件。

For some reason, taking the approach above is not working. 由于某些原因,采用上述方法不起作用。

I am not seeing the subtitles appear. 我没有看到字幕出现。

I think I'm doing something wrong ... think I need some sort of streaming technique to stream the text to the src attribute. 我想我做错了什么...我想我需要某种流技术来将文本流到src属性。

Any ideas or suggestions? 有什么想法或建议吗?

Thanks, 谢谢,

Philip 菲利浦

So src expects a URI. 因此src需要一个URI。 How about encoding the data as a data URI ? 如何将数据编码为数据URI

public static string GetDataUri(string text, string contentType)
{
    var bytes = Encoding.UTF8.GetBytes(text);
    var b64String = Convert.ToBase64String(bytes);

    //the old way
    //var dataUri = string.Format("data:{0};base64,{1}", contentType, b64String);

    //C#6.0 and above
    var dataUri = $"data:{contentType};base64,{b64String}";

    return dataUri;
}

then generate the URI 然后生成URI

//maybe subs have a different content type? I don't know
var subtitleSrc = GetDataUri(theText, "text/plain"); 

and get it into your model and use the value directly as the src for your track element. 并将其放入模型中,并将该值直接用作track元素的src

If it were me, I'd use src = "@Html.Raw(theDataUri)" in your view so that razor doesn't try to meddle with the string before rendering it. 如果是我,我会在您的视图中使用src = "@Html.Raw(theDataUri)" ,以便razor在渲染字符串之前不会尝试插入该字符串。

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

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