我想流式传输在线视频..但我面临j查询错误不允许加载本地资源: ...我找到了很多解决方案,但这一切都不适合我。 这是我的代码......
控制器==>>>
public ActionResult PlayVideo(string VidPath)
{
ViewData["VidPath"] = VidPath;
return View();
}
查看==>>>
<video width="500" height="281" controls>
<source src="@ViewData["VidPath"]" type="video/mp4">
</video>
答案 0 :(得分:0)
我遇到了同样的问题。每当我使用Server.MapPath
或Path.GetFullPath
或只是硬编码“〜/ Content / images / image.jpg”时,我总是得到相同的错误,不允许加载本地资源。
它为我工作的唯一方式是当我使用“Content / images / image.jpg”时没有
〜/
开头
所以我的解决方法就像这样
var imageName = Path.GetFileName(image);
var parentImage = Directory.GetParent(image);
var parentOrder = Directory.GetParent(parentImage.FullName);
var parentDate = Directory.GetParent(parentOrder.FullName);
<img src="Content/Images/@parentDate.Name/@parentOrder.Name/@parentImage.Name/@imageName" />
它有点乱,但它对我有用。或者,您也可以在ViewModel中传递Variable路径以使代码更清晰。
如果有人知道更好的解决方案,请在此处发布,以便人们知道正确的答案。
答案 1 :(得分:0)
您只需要将所有图像/视频网络路径(或本地路径)替换为存储的已编码HTML字符串中的字节字符串即可。 为此,您需要HtmlAgilityPack将HTML字符串转换为HTML文档。 https://www.nuget.org/packages/HtmlAgilityPack
查找以下代码,将每个图像src网络路径(或本地路径)转换为字节字符串。 它肯定会在IE,chrome和firefox中显示具有网络路径(或本地路径)的所有图像。
string encodingHtmlString = Emailmodel.DtEmailFields.Rows [0] [“ Body”]。ToString();
// Decode the encoded string.
StringWriter myWriter = new StringWriter();
HttpUtility.HtmlDecode(encodedHtmlString, myWriter);
string DecodedHtmlString = myWriter.ToString();
//find and replace each img src with byte string
HtmlDocument document = new HtmlDocument();
document.LoadHtml(DecodedHtmlString);
document.DocumentNode.Descendants("img")
.Where(e =>
{
string src = e.GetAttributeValue("src", null) ?? "";
return !string.IsNullOrEmpty(src);//&& src.StartsWith("data:image");
})
.ToList()
.ForEach(x =>
{
string currentSrcValue = x.GetAttributeValue("src", null);
string filePath = Path.GetDirectoryName(currentSrcValue) + "\\";
string filename = Path.GetFileName(currentSrcValue);
string contenttype = "image/" + Path.GetExtension(filename).Replace(".", "");
FileStream fs = new FileStream(filePath + filename, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
br.Close();
fs.Close();
x.SetAttributeValue("src", "data:" + contenttype + ";base64," + Convert.ToBase64String(bytes));
});
string result = document.DocumentNode.OuterHtml;
//Encode HTML string
string myEncodedString = HttpUtility.HtmlEncode(result);
Emailmodel.DtEmailFields.Rows[0]["Body"] = myEncodedString;
答案 2 :(得分:-1)
我在这条线上也遇到了同样的问题。我忽略了将css和js文件添加到静态文件wwwroot。我在项目中添加了静态文件,这些问题已解决。 Solve point
<link href="~/css/bootstrap.min.css" rel="stylesheet" />