How to catch image url using c# - c#

I am making a web application.
In main page I want to show some items in Sharepoint List.
So I saved content data(Included text and image source url) and tried to show it.
But I don`t know how to catch image source url.
My Code is Like this.
string itemBody = string.Empty;
//listItemCol is SPListItemCollection
itemBody = listItemCol[i]["Body"] == null ? "" : listItemCol[i]["Body"].ToString();
I checked itemBody, and it contained this data.
<div class="ExternalClass0E1F6FC2B36A47AEB474512CAE7D8B6E">
<p><img alt="group-news__thumb.jpg" src="/SiteAssets/Lists/Announcement/AllItems/group-news__thumb.jpg" style="margin:5px;" /> </p>
<p>News TEST</p>
<p> </p>
</div>
How can I get that image source using c#? Please help me

You can try this.
int stratIndex = itemBody.IndexOf("src=\"");
int endIndex = itemBody.IndexOf("\"", stratIndex + 5);
string result = itemBody.Substring(stratIndex + 5, endIndex - stratIndex - 5);

Related

Problem with subnode's text when scraping data from a website with HtmlAgilityPack

Hope somebody can help this newbie.
I tried many paths for this subnodes but i cant figure it out.
Html part:
<div class="center-block"> == $0
<div class="match-time" id="dvStatusText">MS</div>
<div class="match-score" id="dvScoreText">4 - 0</div>
<div class="hf-match-score" id="dvHTScoreText">İY : 3- 0</div>
</div>
My code:
Uri url = new Uri("http://arsiv.mackolik.com/Mac/3213138/");
WebClient client = new WebClient();
client.Encoding = Encoding.UTF8;
try
{
string html = client.DownloadString(url);
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
HtmlNodeCollection results = doc.DocumentNode.SelectNodes("//*[#class='center-block']"); //
if (results != null)
{
for (int i = 0; i < results.Count; i++)
{
var t1 = results[i].SelectSingleNode("//*[#class='match-score']").InnerText; // (FT)
var t2 = results[i].SelectSingleNode("//*[#id='dvHTScoreText']").InnerText; // ht
listBox1.Items.Add(t2.ToString());
}
}
My problem from InnerHtml result:
<div class="match-time" id="dvStatusText">MS</div>
<div class="match-score" id="dvScoreText">4 - 0</div>
<div class="hf-match-score" id="dvHTScoreText"></div> // this element has always contains text.
I tried different ways to solve this problem but i have nothing. I can scrape "class=match time" or "class=match-score". But i cant "class=hf-match-score" . I have tried scrape with class or id. Different ways same problem.
Please show me a way. Thanks alot.
The score at half-time is displayed with Javascript. You'll need Selenium or a similar tool to access this element.
As an alternative, you can fetch the data directly from the JSON loaded in the background.
Piece of code in Python (I suppose you can do the same in c#) :
import requests
from lxml import html
# We set up the download url (obtained in the network tab of the developer tool) and the mandatory header
url = 'http://arsiv.mackolik.com/Match/MatchData.aspx?t=dtl&id=3213138&s=0'
hd = {'Referer': 'http://arsiv.mackolik.com/Mac/3213138/'}
# We download and parse the json
data = requests.get(url,headers=hd)
val= data.json()
# We extract values of interest
print(val["d"]["s"],val["d"]["ht"],sep="\n")
Output :
4 - 0
3 - 0

C# - Check URL is valid for TemplateString

I'm currently using a TemplateString to load an image with a link, where {Url} is the file name obtained from the database.
Here is the code I use, as a reference:
Ext.Net.TemplateColumn TheColumn = new Ext.Net.TemplateColumn();
TheColumn.Text = "Image";
TheColumn.DataIndex = "Url";
TheColumn.Align = Alignment.Center;
TheColumn.Flex = 1;
TheColumn.MinWidth = 70;
TheColumn.TemplateString = "<a href='http://example.com/{Url}' data-lightbox='{Url}'><img style='width:60px;height:60px;' src='http://example.com/{Url}'/></a>";
What I would like to do is that I could check if the requested image exists, and if it doesn't, I use an alternative TemplateString instead.
Since I'm using jQuery, I´m leaving the tag in case. The project is an ASP.NET website.

How do I turn results in ASP.NET Web Forms application into URLs?

I'm creating an ASP.NET web form application that searches PDFs for text, if the PDF has that text, it returns the name of the file and the path in a text box. So far everything works but now I'm ready to take that full path and put it inside a link.
So for example, a result would be:
https:\\www.mysite.com\example1.PDF
I want to show a hyper link in the text box that is clickable and simply labeled as:
Example 1
I wonder if I made a mistake in going with ASP.NET Web Forms to do this since it only has a Text Box form control and not a rich text box. The thing is my results are being stored in a string builder so it's not like I just need one static label to be a URL.
Here is what I have:
foreach (var f in files)
{
string pdfSearchMatch = ReadPdfFile(f.File, txtBoxSearchString.Text);
if (pdfSearchMatch != null)
{
string fileNameOnly = Regex.Replace(pdfSearchMatch, #"\\\\my\.test\.site#SSL\\Home\\Sections\\PDFs\\Courses\\.+?\\.+?\\.+?\\", "");
myCommand.CommandText = "select cc.pk1 as 'pid', f.file_name as 'xid', f.link_name as 'linkname' from course_contents cc join course_main cm on cc.crsmain_pk1 = cm.pk1 join course_contents_files ccf on cc.pk1 = ccf.course_contents_pk1 join files f on ccf.files_pk1 = f.pk1 where cm.course_id = 'COM-Syllabi' and f.link_name = '"+fileNameOnly+"'";
using (SqlDataReader sqlReader = myCommand.ExecuteReader())
{
while (sqlReader.Read())
{
string rid = Regex.Replace(sqlReader["xid"].ToString(), "/xid", "rid");
string pdfHyperlink = #"https://my.test.site/Home/pid-"+sqlReader["pid"].ToString()+"-dt-content-"+rid+sqlReader["xid"].ToString();
sb.AppendLine(pdfHyperlink);
}
}
}
}
myConnection.Close();
txtBoxResults.Text = sb.ToString();
I want the results that get assigned to txtBoxResults.Text to be clickable links.
Thanks.
This is the smallest and quickest fix for your code , but it's inappropriate
whereever you have your textbox , just create an
<asp:Literal ID="Literal1" runat="server" ></asp:Literal>
and the exact same line
txtBoxResults.Text = sb.ToString();
replace with
Literal1.Text = sb.ToString();
See how it shows , haven't tested . Still would suggest to resolve the SQL Injection and extra load on the server connection keeping it open.
UPDATE:-
Add the hyperlink part try replace with this the same line , ay face issues with the escape
string pdfHyperlink = #"<a href='https://my.test.site/Home/pid-" + sqlReader["pid"].ToString() + "-dt-content-" + rid + sqlReader["xid"].ToString()+"'>Test</a>";

Loading Image to Image Control using JavaScript

Now I need to change the displaying image to image control. Help me?
var imlocation1 = "Bhopal/";
var currentdate1 = 0;
var image_number1 = 0;
function ImageArray1(n) {
this.length = n;
for (var i = 1; i <= n; i++) {
this[i] = ' '
}
}
image1 = new ImageArray1(3)
image1[0] = '2.jpg'
image1[1] = '4.jpg'
image1[2] = '1.jpg'
var rand1 = 100/ image1.length
function randomimage1() {
currentdate1 = new Date()
image_number1 = currentdate1.getSeconds()
image_number1 = Math.floor(image_number1 / rand1)
return (image1[image_number1])
}
document.write("<img src='" + imlocation1 + randomimage1() + "'>");
"Img" element have "src" attribute that points to the image on a server. So you need to find the "img" element, and if found set the src attribute.
Basic search for whole question http://www.bing.com/search?q=I+Load+an+Image+to+an+Image+Control+using+JavaScript&src=IE-SearchBox&FORM=IE8SRC give following link: http://www.javascriptexamples.org/2011/01/18/how-to-dynamically-load-an-image-using-javascript/
Sample from the link above:
<div id="imageContainer"></div>
var img = document.createElement("img");
img.onload = function(e) {
var container = document.getElementById("imageContainer");
container.appendChild(e.target);
}
img.setAttribute("src","images/puppy.jpg");
$("#YourImageId").attr("src", "ImagePath")
If you use webforms - probably
$("#<%=YourImageServerId.ClientID%>")
If it doesn't help - post some code or some more info to your question
The idea of a control only really exists on the server side. Javascript in this case only runs on the client side, on the browser. Once the server renders the image control, you end up with an IMG tag on the client side. If you tag it with an ID or a CSS class, you can find it with Javascript (or even better a library like JQuery) and load images onto it with that.

Web Part to view photos from SharePoint Library

I am trying to develop a web part that will display a random photo from my photo library.
I have been able to successfully loop through and select a random photo from the library. My issue now is to display this photo on the SharePoint site. I have tried creating an image control and setting the url to the url I retrieve and programmatically add an image control but the image does not seem to display on the page after the web part loads. It simply shows a red X saying the image could not be displayed.
If I navigate to the URL of the photo in the browser it is diplayed but not when the web part tries to pull it down.
Can anybody give me a pointer? I'm still a SharePoint beginner
Here is what I have so far:
Random randomNumber;
randomNumber = new Random();
Controls.Clear();
ClearChildState();
SPWeb thisWeb = SPContext.Current.Web;
SPPictureLibrary pictures = (SPPictureLibrary)thisWeb.Lists["Houston Photos"];
int pictureCount = pictures.ItemCount;
int index = randomNumber.Next(pictureCount);
string source = thisWeb.Url + "/" + pictures.Items[index].Url;
String itemHtml = String.Format(#"<img SRC={0}+height=200px> </img>",source);
this.Controls.Add(new LiteralControl(itemHtml));
imgControl.ImageUrl = source;
I'd say it's your img tag that's the problem
try something like
string itemHtml = string.concat("<img src=\"", source, "\" height=\"200\" />);
You could also use the Image class control, http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.image.aspx
Random randomNumber;
randomNumber = new Random();
Controls.Clear();
ClearChildState();
SPWeb thisWeb = SPContext.Current.Web;
SPPictureLibrary pictures = (SPPictureLibrary)thisWeb.Lists["Houston Photos"];
int pictureCount = pictures.ItemCount;
int index = randomNumber.Next(pictureCount);
string source = thisWeb.Url + "/" + pictures.Items[index].Url;
var image = new Image();
image.ImageUrl = source;
image.Height = 200;
this.Controls.Add(image);

Categories