dispaly image from remote server on the page ASP.Net MVC - c#

I want to display the photo through the Path which is saved on the database Like this \\94.19.247.273\C$\\t\q\laptop.com\bb\images\UrunImages\a1\14-09-18-_04-17-00.jpg .
I have mothod to convert the path
public FileContentResult ImagePath(string path)
{
string str1 = "C:/";
string str2 = "//94.19.247.273/C$//";
string result = path.Replace(str2, str1);
byte[] imageArray = System.IO.File.ReadAllBytes(result);
return new FileContentResult(imageArray, "image/png");
}
I get it from Stackoverflow and on the View like this
#foreach (var item in Model.ProductPhotoPathList)
{
<li data-thumb="#Url.Action("ImagePath","Home",new { path = item})">
<div class="thumb-image"> <img src="#Url.Action("ImagePath","Home",new { path = item})" data-imagezoom="true" class="img-responsive" alt=""> </div>
</li>
}
but here is a problem when page load ImagePath method work successfully but on the page i still get the Path like which is saved on the database \\94.19.247.273\C$\\t\q\laptop.com\bb\images\UrunImages\a1\14-09-18-_04-17-00.jpg, so i can not get changed path on the page.I want to get path like this :C:\t\q\laptop.com\bb\images\UrunImages\a1\14-09-18-_04-17-00.jpg
what the problem might be?
i have this path
/Home/ImagePath?path=%2F%2F94.19.247.273%2FC%24%2F%2FBaburtechAnakartImage%2FACER_murtaza%20test1%2F1_ACER_murtaza%20test1_12-09-18-_11-20-02.jpg.
so i want to convert it to /Home/ImagePath?path=C%3A%2FBaburtechAnakartImage%2FACER_murtaza%20test1%2F1_ACER_murtaza%20test1_12-09-18-_11-20-02.jpg.
the method convert it correctly but i can not get it on the page.

1) Add this on the top of your razor
#{
var split = "httpdocs/";
}
2) Then change your li like
<li data-thumb="#Url.Content("~/" + item.Substring(item.IndexOf(split) + split.Length))">
<div class="thumb-image"> <img src="#Url.Content("~/" + item.Substring(item.IndexOf(split) + split.Length)) " data-imagezoom="true" class="img-responsive" alt=""> </div>
</li>

Str1 and str2 use forward slashes (/), your path has backslashes (\)

Related

In ASP.net, what kind of IF statement could I use to hide a div if the image inside it matches the current page URL?

This is within Sitefinity if that matters, and I am really new at ASP.NET and C#.
I have an image-based navigation element at the bottom of a page that links to different articles using the same template. There are 5 articles, and I would like the link to the active page/article to be hidden so there is a grid of 4 image links.
Here's a screenshot:
https://i.imgur.com/PG2Sfpo.png
Here is the code behind it:
#{
string navTitle = string.Empty;
string url = string.Empty;
if (Model.CurrentSiteMapNode != null && Model.CurrentSiteMapNode.ParentNode != null)
{
if (Model.CurrentSiteMapNode.Title == "Home")
{
navTitle = Model.CurrentSiteMapNode.ParentNode.Title;
}
else
{
navTitle = Model.CurrentSiteMapNode.Title;
}
url = Model.CurrentSiteMapNode.ParentNode.Url;
}
}
<div class="foundation-stories-container">
#foreach (var node in Model.Nodes)
{
#RenderRootLevelNode(node);
}
</div>
#*Here is specified the rendering for the root level*#
#helper RenderRootLevelNode(NodeViewModel node)
{
string[] thisPage = (node.Url).Split('/');
string thisImage = thisPage[4] + ".jpg";
<a href="#node.Url" target="#node.LinkTarget">
<div class="foundation-story-block">
<div class="hovereffect">
<img src="[OUR WEBSITE URL]/stories/#thisImage" class="img-fluid">
<div class="overlay">
<h2>#node.Title</h2>
</div>
</div>
</div>
</a>
}
So we're already getting the page URL and image file name
string[] thisPage = (node.Url).Split('/');
string thisImage = thisPage[4] + ".jpg";
Is this as easy as doing the following?
if (thisImage = thisPage)
{
foundation-story-block.AddClassToHtmlControl("hide")
}
Seems easy enough, but I don't know where to start.
I'm better at Javascript, so I do have a JS solution in place for this already, but I'd really like to find a cleaner way to do it.
<script type="text/javascript">
$(document).ready(function() {
var active = window.location.pathname.split("/").pop()
var name = active;
name = name.replace(/-/g, ' ');
jQuery.expr[":"].Contains = jQuery.expr.createPseudo(function(arg) {
return function( elem ) {
return jQuery(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >=
0;
};
});
$("h2:Contains('" + name + "')").closest(".foundation-story-block").addClass("hide");
});
</script>
This exists on the main template page.
Gets the last part of the URL
Sets that as a variable called "name"
Changes the dash to a space if there is one (most of the pages are associated with names so it's like /first-last)
Then it goes and looks at the which is where the title of the page lives, and if it equals the "name" variable, the ".hide" class is added to the block.
Thanks for any help anyone can provide.
You could bind a click event to your elements with the foundation-story-block class. The reason I use .on instead of .click is because when using UpdatePanels the click event won't fire after an UpdatePanel has it's update event triggered - you might encounter a similar problem with your dynamic binding so I used .on to avoid this.
$(".foundation-story-block").on("click", function() {
// Remove the "hide" class from any elements that have it applied
$.each($(".foundation-story-block.hide"), function(index, value) {
// Remove the class using the "this" context from the anonymous function
$(this).removeClass("hide");
});
// Add the "hide" class to the element that was clicked
$(this).addClass("hide");
});
I haven't run this though an IDE so it might not be 100% correct but it will put you on the correct path.
It is possible, yes. Here is how:
...
#{
var hiddenClass = thisImage == thisPage ? "hide" : string.Empty;
}
<div class="foundation-story-block #hiddenClass">
<div class="hovereffect">
<img src="[OUR WEBSITE URL]/stories/#thisImage" class="img-fluid">
<div class="overlay">
<h2>#node.Title</h2>
</div>
</div>
</div>

Xpath to get href that contain id?

I am trying to get all links that contain ids.I have tried for name and price which is working perfectly but not able to get links which is related to that stuff.
For name I am using this code but for getting links it is not working.
//For Name
var name=scorenodesdoc.DocumentNode.SelectNodes("//[contains(#id,'item')]/ul[1]/li1]/span");
//for Links
var Links= doc.DocumentNode.SelectNodes("//a[contains(#id, 'item')]/#href");
xpath for link is://*[#id="item5d86882c07"]/div[1]/div/a
//This is the code I am try to get the href link
<li id="item5d86882c07" _sp="p2045573.m1686.l8" listingid="401689029639" class="sresult lvresult clearfix li" r="1">
<div class="lvpic pic img left" iid="401689029639">
<div class="lvpicinner full-width picW">
<a href="https://www.ebay.com/itm/Microsoft-Xbox-One-X-White-Console-1TB-Forza-Special-Edition-Bundle-White/401689029639?hash=item5d86882c07:g:lgwAAOSwoZJcQY5s" class="img imgWr2">
<img src="https://i.ebayimg.com/thumbs/images/g/lgwAAOSwoZJcQY5s/s-l225.jpg" class="img" alt="Microsoft Xbox One X White Console 1TB & Forza Special Edition Bundle - White'">
</a>
</div>
</div>
</li>
Ok this is how I resolve my issue.First it gets get anchor tag information then by using getattributevalue to get value of href.
var URLnodes = doc.DocumentNode.SelectNodes("//*[contains(#id,'item')]/div[1]/div/a");
var AllURL = URLnodes.Select(node => node.GetAttributeValue("href",null));

Read Value from span

i want to try a programm which reads out Values of a website with Geckofx. Now i have the following Problem that i dont get the needed Values and it shows to me it is null.
The HTML Code i want to access:
<li id="box" class="tooltip" title="">
<div class="classname"></div>
<span class="value">
<span id="class_test" class="">48.066</span>
</span>
</li>
48.066 is the Value i want to read.
I searched now for about 2 days for a solution that i can go on with my private project i hope anyone can help me :)
Solutions i tried:
Test 1:
GeckoElement testelement = null;
testelement = (GeckoElement)Browser.Document.GetElementById("class_test");
string text = testelement.GetAttribute("value");
Test 2:
GeckoHtmlElement testelement = null;
testelement = (GeckoHtmlElement)Browser.Document.GetHtmlElementById("class_test");
string text = testelement.InnerHtml;
If testelement is null, you're not loading the page correctly, or the source is incorrect.
This works fine:
string content = "<html><body><li id=\"box\" class=\"tooltip\" title=\"\">"+
"<div class=\"classname\"></div>" +
"<span class=\"value\">" +
"<span id=\"class_test\" class=\"\">48.066</span>" +
"</span></li></body></html>";
webBrowser1.LoadHtml(content, "http://www.example.com");
And in webBrowser_DocumentCompleted:
GeckoElement testelement = null;
testelement = (GeckoElement)webBrowser1.Document.GetElementById("class_test");
string text = testelement.InnerHtml; // 48.066
Most likely, you need to wait for the document to finish loading before looking for elements.
Browser.DocumentCompleted += (sender, e) =>
{
var testElement = Browser.Document.GetElementById("class_test") as GeckoElement;
// TODO: handle testElement being null
string text = testElement.GetAttribute("value");
}

Reading a textstring from a webpage

Currently I'm trying to read out a text from a Website via a c# program.
To be exact the Track and the Dj from www.hardbase.fm.
This is what the page source looks like:
<div id="Moderator">
<div id="Moderator_special">
<div style="width:158px; float:left; margin:8px"></div>
<div id="onAir" style="width:420px;overflow:hidden;">
<strong>
<a href="/member/46069" target="_top">
<span style="color:#4AA6E5">BIOCORE</span>
</a>
<span style="color:#26628B"> mit "This Is BIOCORE" (Hardstyle)</span>
</strong>
</div>
</div>
</div>
The text I want to read out is "BIOCORE" and "mit "This Is BIOCORE" (Hardstyle)"
(the text seen when running the snippet).
If have tried the following:
System.Net.WebClient wc = new System.Net.WebClient();
byte[] raw = wc.DownloadData("http://www.hardbase.fm/");
first = webData.IndexOf("#4AA6E5\">") + "#4AA6E5\">".Length;
last = webData.LastIndexOf("</span></a><span style=\"color:#26628B\">");
hb_dj = webData.Substring(first, last - first);
But this doesn't always works because sometimes the source code of the page changes a bit. Like the color or so. And then the search wont work.
So the question is: Is there a better method to do this?
You should try the HTML Agility Pack
HtmlWeb page = new HtmlWeb();
HtmlDocument document = page.Load("http://www.hardbase.fm/");
var nodes = document.DocumentNode.SelectNodes("//[#id='onAir']");
var nodes2 = nodes.Select(c1 => c1.SelectNodes("span")).ToList();
var span1=nodes2[0];
var span2 nodes2[1]

using ffmpeg in asp.net razor (C#)

I know questions like this are all over the internet but I believe my case is different, I have a ASP.NET Webpage (Razor C#) and I have a whole video sharing website ready BUT I still don't understand, How can I convert videos to mp4 and webm using ffmpeg on the upload page and instead of having the original saved and added to the database i want the converted version added. heres my upload page code:
#{
WebSecurity.RequireAuthenticatedUser();
var db = Database.Open("PhotoGallery");
var fileName = "";
var uploader = WebSecurity.CurrentUserId;
var date = DateTime.Now.Date;
var extention = "";
if(IsPost){
var numFiles = Request.Files.Count;
if(numFiles <= 0){
ModelState.AddError("fileUpload", "Please specify at least one photo to upload.");
}
else{
var fileSavePath = "";
var uploadedFile = Request.Files[0];
fileName = Path.GetFileNameWithoutExtension(uploadedFile.FileName).Trim();
extention = Path.GetExtension(uploadedFile.FileName).Trim();
string ndom = Path.GetRandomFileName();
var none = ndom.Remove(ndom.Length - 4);
fileSavePath = Server.MapPath("~/Images/userpics/" +
none + extention);
uploadedFile.SaveAs(fileSavePath);
var insertCommand = "INSERT INTO Videos (FileTitle, UploadDate, UserId, ext, Name) Values(#0, #1, #2, #3, #4)";
db.Execute(insertCommand, fileName, date, uploader, extention, none + extention);
Response.Redirect(Href("~/Photo/View", db.GetLastInsertId()));
}
}
}
<h1>Upload Video</h1>
<form method="post" enctype="multipart/form-data">
#Html.ValidationSummary("Unable to upload:")
<fieldset class="no-legend">
<legend>Upload Photo</legend>
#FileUpload.GetHtml(addText: "Add more files", uploadText: "Upload", includeFormTag: false)
<p class="form-actions">
<input type="submit" value="Upload" title="Upload photo" />
</p>
</fieldset>
</form>
<p class="message info">
The maximum file size is 50MB.
</p>
I think your best bet will be to take the upload as-is, and have an offline application or service poll your table for new records and convert them then. When finished, it would update the table with the new file name and a flag indicating it was converted.
I had a similar project and this is what I did.
It's probably not a good idea to try converting these on the fly. If you a fair amount of simultaneous uploads/conversions, you could end up cratering your server.

Categories