I tried to save rendered C# web browser to my hdd. But it only save html source only, no css or jquery js file. I tried with 3 methods.
It only creates only html file without any css, and jquery js files.
File.WriteAllText(myDocumentPath, webBrowser5.Document.Body.Parent.OuterHtml, Encoding.GetEncoding(webBrowser5.Document.Encoding));
2.It only creates content text. No html, css, js
File.WriteAllText(#"text.txt", webBrowser5.Document.Body.InnerText);
3.It creates bigger html file, still no css or jquery js files
writer.Write(webBrowser5.DocumentText);
private void button1_Click(object sender, EventArgs e)
{
String source = ("viewsource.html");
StreamWriter writer = File.CreateText(source);
String myDocumentPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "\\111.html";
//It only creates only html file without any css, and jquery js files.
File.WriteAllText(myDocumentPath, webBrowser5.Document.Body.Parent.OuterHtml, Encoding.GetEncoding(webBrowser5.Document.Encoding));
//it only creates content text
File.WriteAllText(#"text.txt", webBrowser5.Document.Body.InnerText);
//It creates bigger html file, still no css or jquery js files
writer.Write(webBrowser5.DocumentText);
writer.Close();
}
I found solution on codeproject. MHTML is the way to go.
http://www.codeproject.com/Articles/12977/Harvesting-Web-Content-into-MHTML-Archive
Other solution is save as image:
http://www.codeproject.com/Articles/6601/Capture-an-HTML-document-as-an-image
Related
I'm able to load a local HTML file into my WebView like this (this works fine):
var fileName = "Views/Default.html";
var localHtmlUrl = Path.Combine(NSBundle.MainBundle.BundlePath, fileName);
var url = new NSUrl(localHtmlUrl, false);
var request = new NSUrlRequest(url);
WebView.LoadRequest(request);
I'd like to reference a CSS file (also local) in my HTML file:
<link href="Content/style.css" rel="stylesheet">
The CSS file does exist inside of a Content folder, the build action is set to Content for said file.
How can I reference/load the CSS file in question via my html file? Possible?
UPDATE: Had css and html in separate folders. Put them both in a Content folder then updated the hrefs which solved the issue while using LoadRequest.
Check this link
https://developer.xamarin.com/recipes/ios/content_controls/web_view/load_local_content/
Section Additional information
Html generated in code can also be displayed, which is useful for customizing the content. To display an Html string, use the LoadHtmlString method instead of LoadRequest. Passing the path to the Content directory helps the web view resolve relative Urls in the Html, such as links, images, CSS, etc.
// assumes you've placed all your files in a Content folder inside your app
string contentDirectoryPath = Path.Combine (NSBundle.MainBundle.BundlePath, "Content/");
string html = "<html><a href='Home.html'>Click me</a></html>";
webView.LoadHtmlString(html, new NSUrl(contentDirectoryPath, true));
I loaded a html local file in a WebBrowser control within a WinForm. In the html code, I defined some variables in %variables%.
My question is how to transfer/reference strings/data from WinForm to the %viarables% defined in the loaded html page, and refresh the loaded html page displayed on the WebBrowser.
Following is the code of loading a local html file from. Any suggestion will be appreciated.
By the way, it is a WinForm application, not asp.Net.
string curDir = Directory.GetCurrentDirectory();
this.webBrowser1.Url = new Uri(String.Format("file:///{0}/{1}", curDir + "\\Forms", fileName));
Instead of trying to replace your variables after loading the HTML, which could be problematic, why not read the source file, replace the variables, then load the updated document into the web browser control? This seems much more straightforward.
For example:
string dir = Path.Combine(Directory.GetCurrentDirectory(), "forms");
string html = File.ReadAllText(Path.Combine(dir, fileName));
foreach (string variable in GetListOfVariables())
{
html = html.Replace(variable, GetReplacementForVariable(variable));
}
webBrowser1.DocumentText = html;
Do you have control over contents of the HTML file? You could try InvokeScript method paired with a Javascript function prepared in the document beforehand.
If you don't control the contents, then you can inject javascript from your code. Here are some examples of doing that.
I'm converting a desktop application that hosts HTML content into an online application. I have various large pieces of prebuilt static html that need to be included in an MVC page depending on user actions. Each of the static html pages includes at least one img tag that references a file that, in a Web Forms page would be located in the same directory. Here's a very simplified example:
Static html:
<html>
<!-- Large chunk of html -->
<img src="logo.gif" >
<!-- More html -->
</html>
SampleController:
Dim html as String = GetFileContentAsString("~/Content/Sample/Static.html")
ViewBag.StaticHTML = PrepareHTMLContent(html)
View:
#Html.Raw(ViewBag.StaticHTML)
The result of the above is a page (e.g. http://localhost:12345/Sample) with a broken image link in the middle of the HTML. I'm already preprocessing the html where possible to strip out useless tags and insert Javascript and CSS links but preprocessing the image paths is unreliable because they could be anywhere in the static html and are quite likely to be inconsistent or otherwise quirky.
So how can I place (or create) the image file in the right location for the static html to pick it up? Is there any other option (bearing in mind that I also need to link CSS and JavaScript files and that the static html has a bunch of other files associated with it that need to be kept in a single location)?
Or is there a way to define or override the location the dynamic MVC page is built?
Easiest thing would be to have /Sample return an HTML page that simply loads /~Content/Sample/Static.html into an iframe so that the browser will resolve relative paths in static.html to be within /~Content/Sample/
I have to write a c# class that gets an html page's content (the page is public) and trigger a javascript function that downloads a file.
My goal is to download the file and save it in a folder
The page is a public html page that does not require login.
The link looks like this :
href="javascript:__doPostBack('lbtSpreadsheet','')" style="font-weight: 700">Export Results</a>
the doPostBack function contains the following code:
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
when you click the link manually, it submits the form and returns an excel sheet, the download dialog box opens to ask you where you need to save it.
I want to do this automatically to get the excel sheet and then process it.
I found out that I can find the links on a page like this:
HtmlWeb hw = new HtmlWeb();
HtmlDocument doc = hw.Load(/* url */);
foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[#href]"))
{
}
but how can I trigger the javascript in the link and save the file returned?
Thank you
I don't think you can. C# server side web clients such as HtmlWeb or WebClient only understand HTTP/HTML, they aren't full-fledged web browsers capable of executing javascript the same way that IE, Firefox or Chrome could.
If you want the file download to start automatically during the onload of the page without the Export Results link triggering the file download then you can write the below js script that calls your js function __doPostBack which in turn does the form submission.
window.onload = function() {
__doPostBack (param1, param2);
};
Or as your question title says 'trigger javascript function using c#' then you can access Javascript function from C# code using ScriptManager class which is part of System.Web.UI
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "FileDownload", "Javascript:__doPostBack (param1, param2);", true);
}
I'm creating an html file based on xml and xsl with XslCompiledTransform in c#.net. This works perfectly.
But the xsl also has a css file included, and I'm wondering if there is any way to get this css styles included in the output html file, so it can be showed as a standalone file (so I don't have to copy the css file to wherever i want to see the file).
To define the style of each tag explicitly is not an option either unfortunately, and the file is of course really ugly without the css.
Any help would be very much appreciated! :)
In your output html add a style sheet link within the <head> tag.
<link rel="stylesheet" type="text/css" href="mystyle.aspx" />
Then add a page to your project called mystyle.aspx. In Page_Load of this file you do your xslt transformation to output only the css part. (And remove the css part of the transformation for the html pages).
protected void Page_Load(object sender, EventArgs e) {
Response.Clear();
Response.ContentType = "text/css";
string css = // Do your xslt transformation here
Response.Write( css );
Response.End();
}
If the CSS is the same for all pages, you might want to add some caching to the code above to save doing the transformation every time.
You might have to use some parameters to point to your xml/xslt, but you haven't provided any information in your question in this regard.