0

i want to stream online videos ..but i am facing j query error Not allowed to load local resource: ...i find many solution but that all did not work for me. here is my code...

controller==>>>

 public ActionResult PlayVideo(string VidPath)
        {

            ViewData["VidPath"] = VidPath;
            return View();
        }

view==>>>

<video width="500" height="281" controls>
    <source src="@ViewData["VidPath"]" type="video/mp4">

</video>

i am using chrome browser but i don't know what the exactly problem is..

sajiii
  • 172
  • 1
  • 1
  • 8
  • 3
    Provide a virtual path, not physical one. –  Jul 02 '16 at 11:25
  • I replace to this but error is same.. – sajiii Jul 02 '16 at 11:31
  • I think if you were doing that, it would need to be `scr="@Url.Content(Server.MapPath(......))"` –  Jul 02 '16 at 11:35
  • should i place the physical path inside the brackets of server.mappath?? – sajiii Jul 02 '16 at 11:41
  • try `scr="@Url.Content(Server.MapPath("~App_Data/uploads/"+User.Identity.Name+"/Videos_f65db84d-4‌​b24-4054-a270-bb9afaf1cff8/video-1449306301.mp4"))"` (although the Server.MapPath()` should be done in the controller and the result passed to the view. –  Jul 02 '16 at 11:43
  • ohh its strange to mee...error removed but can't play video bcz in src the path is this .....`C:\Users\sajid khan\Documents\Visual Studio 2015\Projects\WebApplication5\WebApplication5\Home\~App_Data\uploads\sajidishaq007@gmail.com\Vide‌os_f65db84d-4‌b24-4054-a270-bb9afaf1cff8\video-1449306301.mp4` – sajiii Jul 02 '16 at 11:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116256/discussion-between-stephen-muecke-and-sajiii). –  Jul 02 '16 at 11:54

3 Answers3

0

I had the same problem. Whenever I used Server.MapPath or Path.GetFullPath or just hardcoded "~/Content/images/image.jpg" I always got the same error Not Allowed to load local resources.

The only way it was working for me is when I used "Content/images/image.jpg" without the

~/

at the beginning

So my workaround was like this

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" />

It is a little messy but it worked for me. Alternatively, you can also pass in the Variable path in a ViewModel to make a little cleaner code.

If someone knows for a better solution please post it here so people will know the correct Answer.

scg
  • 21
  • 9
0

You just need to replace all image/video network paths(or local path) to byte strings in stored Encoded HTML sting. For this you required HtmlAgilityPack to convert Html string to Html document. https://www.nuget.org/packages/HtmlAgilityPack

Find Below code to convert each image src network path(or local path) to byte sting. It will definitely display all images with network path(or local path) in IE,chrome and firefox.

string encodedHtmlString = 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

I had the same problem for this line. I overlooked add to css and js files to static files, wwwroot. I added static files in project and these problems have solved. Solve point

<link href="~/css/bootstrap.min.css" rel="stylesheet" />
Esin Tekin
  • 57
  • 1