I am going to do my best to ask this question as clearly as possible. Is it possible to write some asp.net code that can go through and read the source of pop up windows that are normally opened by clicking a javascript:void() link. Basically i want to read the source of said popups, extract specific links and then render those links in a web page. What i am trying to achieve is a way to make it easy to download the videos of the Senate floor - their videos open in a new popup that uses silverlight. The mp4 file location is in the source so i want to read that url. I did something similar using the code below. The difference was that the mp4 links were in main page. The code read the source and then outputted the video links so i could just right click and do a save as. This was for the videos at the German security conference. Apologies if this is not seen as a constructive question and ends up being closed.
Protected Sub btnClick_Click(sender As Object, e As EventArgs) Handles btnClick.Click
Dim r As New Regex("\bhttps?://\S+\.(?:jpg|png|gif|mp3|mp4|3gp)\b", RegexOptions.IgnoreCase)
Dim c As New WebClient
Dim s As String = c.DownloadString(txtUrl.Text)
Dim sb As New StringBuilder
For Each m As Match In r.Matches(s)
sb.Append("" & m.Value & "<br />")
divLinks.InnerHtml = sb.ToString
Next
End Sub
Related
Is there a way to search folder name(whether its root folder or subfolder and with files inside) through web vb.net? I searched all over the internet and haven't found single help. this is what i've done so far. but this is in windows forms.
I am looking for web forms. thank you
Protected Sub search_Click(sender As Object, e As EventArgs) Handles Search.Click
Dim MainFldr = "d:\shared\"
Dim SKfiles() As System.IO.FileInfo
Dim FldrInfo As New System.IO.DirectoryInfo(MainFldr)
Dim flpth As String
flpth = ""
ListBox2.Items.Clear()
SKfiles = FldrInfo.GetFiles("*" & txtfolder.Text & "*.*", IO.SearchOption.AllDirectories)
For Each MySearchfile In SKfiles
flpth = ""
flpth = MySearchfile.DirectoryName + "\" + MySearchfile.Name
ListBox2.Items.Add(flpth)
Application.DoEvents()
Next
End Sub
Well, it turns out you can do the same in web land.
However, one huge "issue" to remember?
For any web page url, web page link, web page document reference? You will use the web site URL for that file in question.
So, say you have the web site (root) and then a folder called Images.
images\cat.jpg
So, the path name for the WEB SITE MARK up will be:
"~/images/cat.jpg
however, WHEN you run code behind? (your vb.net code). Now, ALL AND EVERY file reference MUST be a full normal standard windows path name!!!
so, if say your vb.net code was to "test" if the above file existed?
Lets say we drop a button and a image control on a web page - we have this:
and lets just make this super simple - we want in vb code to set the above image control to a picture we have in our Content folder.
Well, you have to translate the web based url into a plane jane good old fashioned windows file path name!
So, you would do this:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strF As String
strF = "~/Content/uncheck1.jpg"
Dim strFInternal = Server.MapPath(strF)
Debug.Print(strFInternal)
If File.Exists(strFInternal) Then
Image1.ImageUrl = strF
End If
End Sub
Note how our INTERNAL code MUST use a good old plane jane windows file path name.
but for things on the web page? They are ALWAYS referenced from the point of view of the web site. After I run the above code I get this:
But that debug.print of the file name? This is the output:
Output:
C:\Users\AlbertKallal\source\repos\WebFriendlyTest\Content\uncheck1.jpg
Note VERY careful the above? It was a plane jane INTERNAL file name. I can't tell you how many times I struggled with above simple concept:
Web page controls and markup: use correct URL path names
Code behind: use full windows path name!!!
So, your posted code? it WILL work near as it stands!!!!
The only issue here is what folder you looking to push out to that list box?
Say in above, I wanted to push out ALL files in that Content Folder.
I mean, I can type in this to display that check box if I wanted to:
So the web page now displays that one simple image on the page - since I typed in the FULL correct url!!!
So, how about we drop a list box on the web form, and when we click our button, we display ALL files in that Content folder. Now I could "guess" the location of the file folder, but for ANY WEB page to use that folder, it HAS to be part of the web site folders (or sub folders). The web site can NOT just out of the blue map to steal or grab ANY file on the computer - those URL's can ONLY point to say root of the site + any sub folder.
So that content folder on this site is this:
So, as you can see, then I can type in a url that is the base site, and then that folder, and then that picture (uncheck1.jpg) that exists in that folder.
So, lets drop in a listbox, and push out all the files from that folder to the listbox on the form:
Our markup will be this:
So, just a button, and a list box.
And the button code for the Content folder? Well, we would use this:
(I removed the search part - but that is standard code - JUST like you ALWAYS used in the past!!!
So:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim MainFldr = "~/Content"
Dim FldrInternal = Server.MapPath(MainFldr)
Dim SKfiles() As System.IO.FileInfo
Dim FldrInfo As New System.IO.DirectoryInfo(FldrInternal)
ListBox1.Items.Clear()
SKfiles = FldrInfo.GetFiles("*.*", IO.SearchOption.AllDirectories)
For Each MyFile In SKfiles
ListBox1.Items.Add(MyFile.Name)
Next
End Sub
I decided to NOT use the full path name - but I could have.
The output:
so just remember, that any web page? It can ONLY use files and things that are PART of the web site. And any url - even for a picture, file etc.? Then it MUST be a valid URL.
However, the code behind? It is NOT limited to JUST the folders and files from the root and sub folders. You can actually use your d: drive, or anything else that box on the network can get/grab/use/fetch. So the code behind is your plane jane windows code.
The web site URL and files from web pages thus can only use folders/files that are part of the web site. However, in some cases, say you had some other big server with folders for pictures and files - but your web site (and valid URL's) need to use that folder? Well, you then setup what is a called a virtual directory. This you quite much need to use IIS as opposed to using IIS express edition. (you can google for express edition + setup virutal folders).
Once that virtual folder is setup? It will look like any other sub folder in teh site - but they are not in the site anymore. (and this is also used for documents or PDF folders often too).
This is simply LOOKS like a sub folder, but it really is a folder that can point to any other folder on your computer network. of course this would only work if the final web server and published site location is running on a box on your server, and say not some hosting plan.
So one should code and NOT think in terms of full valid local server file names, but try to always think in terms of the URL, and the path name, and even path names in sub folders in that web site. But in all cases, the code behind operations are against plane jane valid full path names.
However, if you thought that debug.print of the simply .jpg file name was long or funny? Well, on a published server those file path names can be super crazy paths - but you NEVER care if you reference the folders as per above.
I solve the problem by creating search like below. searching folder name and subfolder.
Dim files As FileInfo() = Nothing
Dim dirInfo As DirectoryInfo = New DirectoryInfo(startFolder)
Dim dir As DirectoryInfo() = Nothing
dir = dirInfo.GetDirectories(folderName, SearchOption.AllDirectories)
For i = 0 To dir.Length - 1
If dir(i).Name = folderName Then
Dim path2 = Directory.GetDirectories(dir(i).FullName)
If path2.Length = 0 Then
files = dir(i).GetFiles()
For Each file In dir(i).GetFiles()
path = CStr(dir(0).FullName)
path = path.Remove(0, 11)
HiddenField1.Value = path & "\" & file.Name
HiddenField2.Value = file.Name
Dim tr As New TableRow
tr.Cells.Add(New TableCell With {.Text = file.Name})
grd.Rows.Add(tr)
Dim tdlnk As New TableCell
Dim lnk As New HtmlAnchor
tdlnk.Controls.Add(lnk)
tr.Cells.Add(tdlnk)
Next
Exit For
Else
Dim aaa As String = path2(0).Substring(path2(0).Length - 10)
startFolder = path2(0).ToString
folderName = aaa
Dim files1 As FileInfo() = Nothing
Dim dirInfo1 As DirectoryInfo = New DirectoryInfo(startFolder)
Dim dir1 As DirectoryInfo() = Nothing
dirInfo1.GetDirectories(folderName, SearchOption.AllDirectories)
files1 = dirInfo1.GetFiles
For Each file In dirInfo1.GetFiles
path = CStr(dirInfo1.FullName)
path = path.Remove(0, 11)
HiddenField1.Value = path & "\" & file.Name
HiddenField2.Value = file.Name
Dim tr As New TableRow
tr.Cells.Add(New TableCell With {.Text = file.Name})
grd.Rows.Add(tr)
Dim tdlnk As New TableCell
Dim lnk As New HtmlAnchor
tdlnk.Controls.Add(lnk)
tr.Cells.Add(tdlnk)
Next
Exit For
End If
End If
Next
In my ASP.net web application I have a page which shows a (not very complicated) table. I have implemented a button so when the (registered) members clicks on it, the browser downloads the table as an Excel document
The code to generate this Excel is as follow:
Dim response As HttpResponse = HttpContext.Current.Response
' first let's clean up the response.object
response.Clear()
response.Charset = ""
Dim filename As String = MapPath("~/ex1.xls")
' set the response mime type for excel
response.ContentType = "application/vnd.ms-excel"
response.AddHeader("Content-Disposition", "attachment;filename=""" & filename & """")
' create a string writer
Using sw As New StringWriter()
Using htw As New HtmlTextWriter(sw)
' instantiate a datagrid
Dim dg As New DataGrid()
dg.DataSource = dt
dg.DataBind()
dg.RenderControl(htw)
response.Write(sw.ToString())
response.[End]()
End Using
End Using
The problem is that this code is breaking somehow the ASPN.net LoginStatus as when you try to log out after downloading this Excel (the "log out" buttom is always visible at the application) every click on the buttom will download the same Excelover and over (once per click). If you reload window or change page the "log out" will work again.
this is the "log out" link when inspecting it with chrome:
<a id="ctl00_ctl00_MainContent_LoginStatus1" href="javascript:__doPostBack('ctl00$ctl00$MainContent$LoginStatus1$ctl00','')">Log Out</a>
I have tried to make the Excel to download from other page using:
onclick="window.document.forms[0].target='_blank';"
and now it opens a new tab, download the Excel, close the tab (fast process) but still keeps breaking the logout link.
And I run out of ideas, any help, code, hint or whatever possible solution to fix this problem is very welcome.
Open a new page within the blank target, and let the new page generate the excel
This looks like a server side error.
Your code to logout actually runs the code to download Excel.
May be you should throw in a 'Response.End();' in the click handler for logout.
Well a quick work around would be to use report viewer control, which has an export to excel functionality.
I know this solution is not exactly what you were looking for, but from my experience when a user is looking for excel functionality, instead of providing a table/gridview results to the user i provide an rdlc report, which has all the export functionality.
I am thinking about using a WebBrowser object to create a screen shot of a page a user has visited (by capturing the URL). Part of the class will look something like tHE below. This is an internal application and the reason is to allow the user to see how the dynamic page looked several months ago when they last visited.
Public Function ConvertPage(ByVal PageUrl As String) As Bitmap
Me.PageUrl = PageUrl
Dim thrCurrent As New Thread(New ThreadStart(AddressOf CreateImage))
thrCurrent.SetApartmentState(ApartmentState.STA)
thrCurrent.Start()
thrCurrent.Join()
CreateImage()
Return ConvertedImage
End Function
Private Sub CreateImage()
Dim BrowsePage As New WebBrowser()
BrowsePage.ScrollBarsEnabled = False
BrowsePage.Navigate(PageUrl)
AddHandler BrowsePage.DocumentCompleted, AddressOf _
WebBrowser_DocumentCompleted
While BrowsePage.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
End While
BrowsePage.Dispose()
End Sub
Earlier today I was reading an entry (I think it was on here) and the answerer advised the questioner to avoid this approach. I do not have a link to this post. Is this a poor apprach bin your view i.e. using a WebBrowser object in an ASP.NET page?
I think this is the link you are after.
problem using winforms WebBrowser in asp.net
This code project page seems to have a solution and talks about the advantages of it
http://www.codeproject.com/Articles/50544/Using-the-WebBrowser-Control-in-ASP-NET
His solutions seems to require three threads to use the control;
perhaps a better options would be to take a via Javascript and then post the information back via an ajax call, have a look at this Javascript library that does it
http://html2canvas.hertzen.com/
if you are going with the webbrowser approach att the bottom of this question is c# code for capturing the image
Take a screenshot of a webpage with JavaScript?
I am busy creating a control in asp.net. The control consists of some text and an image. The image will be used to display a graph. The control gets the data for the graph by reading it from a database. It seems I have two options to display the graph in the image box. First, I can create a jpg from the data and save the file on the server. I can then load this file into the image box. I think this would cause a problem if various users try to access the site at the same time, so I don't think it is a good option. The other options is to create a file called output, that outputs the graph like this: objBitmap.Save(Response.OutputStream, ImageFormat.Gif)
I can then display the image like this in my control: Image1.ImageUrl = "output.aspx"
The problem that I am facing is how do I get the data from my control to the output page? As far as I know it is too much data to pass as a parameter. If there is another better method of doing it, please let me know
Thanks
You can write the image content to the response of output.aspx. Something like this (taken from one of my existing GetImage pages:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strFilename As String
strFilename = Request.QueryString("filename")
Dim strPath As String = AppSettings("ProxyImageSavePath") & strFilename
Response.Clear()
Response.ContentType = "image/jpeg"
If Not IO.File.Exists(strPath) Then
'erm
Response.Write("file not found")
Else
Response.WriteFile(strPath)
End If
Response.End()
End Sub
Edit: I've just realised this may not be the solution you're looking for; Your webserver should be able to cache this though.
You need handlers. See in this article, Protecting Your Images, section "Creating the ImageHandler HTTP Handler", you'll find the code how to write the handler, so it outputs an image. You don't need all, it's only an example.
You also need to register the handler in the web.config.
To reference it Image1.ImageUrl = "handler.ashx?graphdata=xxxx". Use QueryString to get the data source to generate the graph.
Howdy, Now I'm a complete newby to .Net and the likes of VB (I'm more of a Ruby/Rails, PHP kinda guy).
Anyway I currently have a sub that has a string builder that I'm wish to use as the content of a CSV which I will email later from a webpage. My stringbuilder is creating the content for the CSV but I don't know how to create the CSV dynamically (I don't want the CSV on disk).
Private Shared Sub BuildClientTrustBalanceCSV(ByVal sb As StringBuilder, ByVal clientsWithTrustBalance As List(Of NonPaymentHistory))
If clientsWithTrustBalance Is Nothing OrElse clientsWithTrustBalance.Count = 0 Then
Exit Sub
End If
' need to somewhere create the CSV then inject the StringBuilder into it?
With sb
' create the CSV
For Each zeroBalanceClient As NonPaymentHistory In clientsWithTrustBalance
.AppendFormatLine("{0},", zeroBalanceClient.Number)
Next zeroBalanceClient
End With
End Sub
Any ideas? I know I should be able to sort this but .NET just isn't my thing?
Here is a link that might be helpful, but I agree with Oded that the question is misleading.
How do I best generate a CSV (comma-delimited text file) for download with ASP.NET?