I am developing an application which is showing web pages through a web browser control.
When I click the save button, the web page with images should be stored in local storage. It should be save in .html format.
I have the following code:
WebRequest request = WebRequest.Create(txtURL.Text);
WebResponse response = request.GetResponse();
Stream data = response.GetResponseStream();
string html = String.Empty;
using (StreamReader sr = new StreamReader(data))
{
html = sr.ReadToEnd();
}
Now string html contains the webpage content. I need to save this into D:\Cache\
How do i save the html contents to disk?
You can use this code to write your HTML string to a file:
var path= #"D:\Cache\myfile.html";
File.WriteAllText(path, html);
Further refinement: Extract the filename from your (textual) URL.
Update:
See Get file name from URI string in C# for details. The idea is:
var uri = new Uri(txtUrl.Text);
var filename = uri.IsFile
? System.IO.Path.GetFileName(uri.LocalPath)
: "unknown-file.html";
you have to write below code on save button
File.WriteAllText(path, browser.Document.Body.Parent.OuterHtml, Encoding.GetEncoding(browser.Document.Encoding));
Now the 'Body.parent' must save whole the page instead of just saving only part.
check it.
There is nothing built-in to the .NET Framework as far I know.
So my approach would be like below:
Use System.NET.HttpWebRequest to get the main HTML document as a
string or stream (easy). (Which you have done already)
Load this into a HTMLAgilityPack document where you can now easily
query the document to get lists of all image elements, stylesheet
links, etc.
Then make a separate web request for each of these files and save
them to a subdirectory.
Finally update all relevent links in the main page to point to the
items in the subdirectory.
Related
I'm trying to show a pdf in my view that I download from an API, however everytime the view is loaded the browser downloads the pdf instead of showing it inside the iframe or embed.
I'm getting and returning the pdf in my action like this:
public FileStreamResult GetPDFInBrowser(Guid id)
{
string url = A link that gets the pdf from API with id, content is stored as a string inside the "root" object;
WebClient client = new WebClient();
string response = client.DownloadString(url);
Root responseObject = JsonConvert.DeserializeObject<Root>(response);
OOLDocument oolDocument = responseObject.value[0];
byte[] byteArray = Encoding.UTF8.GetBytes(oolDocument.Contents);
MemoryStream stream = new MemoryStream(byteArray);
return File(stream, "application/pdf", oolDocument.FileName);
}
Afterwards I try to show the pdf in my view like this with either iframe or embed:
<iframe src="#baseUrl/GetPDFInBrowser?id=#Model.DocumentsGuid" class="pdfviewer">
</iframe>
<embed src="#baseUrl/GetPDFInBrowser?id=#Model.DocumentsGuid" class="pdfviewer" type="application/pdf" id="pdfObjectViewer">
</embed>
So, how do I make sure the pdf is embedded in the browser instead of having the browser download it?
Any help or tips for improving the code are greatly appreciated.
Edit:
With some more searching combined with trial and error I was able to fix the error I was getting and have therefore removed it from the remainder of the question.
For those interested i fixed the error "Cannot load pdf-document" by changing:
byte[] byteArray = Encoding.UTF8.GetBytes(oolDocument.Contents);
To:
byte[] byteArray = Convert.FromBase64String(oolDocument.Contents);
However the pdf is still being downloaded instead of being shown in the view
How to save a below Xml web page content as a XML document in local drive using C# .Net.
URL is as below :
http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote
I want to load few field values into oracle DB table in a daily basis. Please help me to code.
As far as saving the XML as a local file, I have put together the following code.
System.Net.WebClient wc = new System.Net.WebClient();
string webData = wc.DownloadString("http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote");
System.IO.File.WriteAllText(#"C:\YOUR_FILE_PATH\FILE_NAME.xml", webData);
Just replace YOUR_FILE_PATH and FILE_NAME with where you want to save the data and your desired file name and it should be good.
take a look at XMLDocument class
you can use method Load to load your xml and Save to save it to a file
https://msdn.microsoft.com/en-us/library/system.xml.xmldocument(v=vs.110).aspx
First you need to generate the C# classes for your XML.
https://msdn.microsoft.com/en-us/library/hh371548(v=vs.110).aspx
The summary is you copy the xml text and then in a code file but outside of a class, click menu Edit -> Paste Special -> Paste XML as Classes
https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.110).aspx
Then you can then load the xml file using XmlSerialzer
From there you can use push it to your oracle DB through Entity Framework Connection.
code to deserialize into a list object:
string url = "http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote";
XmlSerializer xmlSerializer = new XmlSerializer(typeof(list));
HttpWebRequest aRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse aResponse = (HttpWebResponse)aRequest.GetResponse();
list mylist = (list)xmlSerializer.Deserialize(aResponse.GetResponseStream());
Then you can use mylist as any object.
I'm trying to get a link from a pastebin. Where the link is the only text in the raw paste. Then I want to download a file from the link in pastebin.
WebRequest request = WebRequest.Create("http://pastebin.com/raw/Dtdf2qMp");
WebResponse response = request.GetResponse();
System.IO.StreamReader reader = new
System.IO.StreamReader(response.GetResponseStream());
Console.WriteLine(reader.ReadToEnd());
WebClient client = new WebClient();
client.DownloadFile (Link gotten from pastebin here, "c:\\File");
System.Threading.Thread.Sleep(5000);
Instead of dumping the text read to console output, you should assign it to a variable.
var pastebinOutput = reader.ReadToEnd();
Then just pass that as the link for the DownloadFile method. If you want to do verification that it's actually a URL you got from the original pastebin, you can look into System.Uri's TryCreate method.
I've got a solution - assuming you have your link in the raw pastebin link (mine is a .txt file saying 'it worked') I suggest you copy and paste the code below exactly - if you get an file saying 'it worked' then you can change the pastebin link & file names. If you don't want to open the file then remove Process.Start - if you want to change the delay just change the number (it's in milliseconds) Also, you can change the format from .txt to .exe or whatever your file is (or you can remove it so its the defualt name in the download link):
WebRequest request = WebRequest.Create("https://pastebin.com/raw/QAWufg1z");
WebResponse response = request.GetResponse();
System.IO.StreamReader reader = new
System.IO.StreamReader(response.GetResponseStream());
var pastebinOutput = reader.ReadToEnd();
WebClient client = new WebClient();
client.DownloadFile(pastebinOutput, #".\downloaded.txt");
MessageBox.Show("File should open automatically in the next minute. Please wait...");
await Task.Delay(3000); //3000 = 3 seconds
Process.Start(#".\downloaded.txt");
I have requirement to save an XML document which is displayed in a HTML page.
The scenario is like this: I am sending search request to the server and in return I am getting xml file but displayed in html page. What I want is to save client-side the xml file displayed inside the html form, using javascript, asp.net (C#).
Please see this link (server return file like this)
http://www.w3schools.com/dom/books.xml
You need to save the stream from your request into a xmldoc.
Should be really straight forward.
Have a look here:
http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.save(v=vs.100).aspx
Also, here there is a microsoft article about it:
http://support.microsoft.com/kb/307643
The sent file should have a HTTP header indicating it's meant to be saved, not shown.
Do it this way:
context.Response.ContentType = "application/force-download";
Assuming you are writing a client app to consume this file
using System.Net;
new WebClient().DownloadFile("http://www.w3schools.com/dom/books.xml", #"c:\books.xml");
As Rutesh said you can't go with javascript. Save it on the server with C#.NET and then access it from server. Secondly, the page you are talking about is not an html page its an xml file. I hope you know how to build an asp.net page, if yes then use this:
XmlDocument xdoc = new XmlDocument();
xdoc.Load("http://www.w3schools.com/dom/books.xml");
xdoc.Save("myfilename.xml");
Happy coding!
sourceResponse.ContentType = "application/octet-stream";
byte[] responseContent = "XML File content"
sourceResponse.AddHeader("content-disposition", String.Format("attachment; filename={0}.xml", "yourFileName"));
sourceResponse.BinaryWrite(responseContent);
sourceResponse.End();
I looking for the easiest way to export the currently viewed asp.net web page to a PDF document using iTextSharp - it can be either a screenshot of it or passing in the url to generate the document. Sample code would be greatly appreciated. Many thanks in advance!
On a past project, we used Supergoo ABCPDF
to do something like you need to and it worked quite well. You basicly feed it an url and it process the HTML page into a PDF.
On the downside, it's a licensed software with costs associated to it and we had some performance issues when exporting a lot of large PDF at the same time.
Hope this helps !
Adding to what Darin said in his comment.
You can try using wkhtmltopdf to generate PDF files. It takes URL as input. This is what I have used for my SO application so2pdf.
It is not that easy (or i think so), i had same problem and i had to write code to generate exact page in pdf. It depends of page and used styles etc. So i create drawing of each element.
For some project i used Winnovative HTML to PDF converter but it is not free.
Give a try with PDFSharp (to generate PDF files at runtime), and it's Open Source library :
http://pdfsharp.com/PDFsharp/index.php?option=com_content&task=view&id=27&Itemid=1
Alternative solution : http://www.bratched.com/en/component/content/article/76-how-to-convert-xhtml-to-pdf-in-c.html
There is an example Convert the Current HTML Page to PDF on WInnovative website which does exactly this. The relevant C# code to convert the currently viewed asp.net web page to a PDF document is:
// Controls if the current HTML page will be rendered to PDF or as a normal page
bool convertToPdf = false;
protected void convertToPdfButton_Click(object sender, EventArgs e)
{
// The current ASP.NET page will be rendered to PDF when its Render method will be called by framework
convertToPdf = true;
}
protected override void Render(HtmlTextWriter writer)
{
if (convertToPdf)
{
// Get the current page HTML string by rendering into a TextWriter object
TextWriter outTextWriter = new StringWriter();
HtmlTextWriter outHtmlTextWriter = new HtmlTextWriter(outTextWriter);
base.Render(outHtmlTextWriter);
// Obtain the current page HTML string
string currentPageHtmlString = outTextWriter.ToString();
// Create a HTML to PDF converter object with default settings
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
// Set license key received after purchase to use the converter in licensed mode
// Leave it not set to use the converter in demo mode
htmlToPdfConverter.LicenseKey = "fvDh8eDx4fHg4P/h8eLg/+Dj/+jo6Og=";
// Use the current page URL as base URL
string baseUrl = HttpContext.Current.Request.Url.AbsoluteUri;
// Convert the current page HTML string a PDF document in a memory buffer
byte[] outPdfBuffer = htmlToPdfConverter.ConvertHtml(currentPageHtmlString, baseUrl);
// Send the PDF as response to browser
// Set response content type
Response.AddHeader("Content-Type", "application/pdf");
// Instruct the browser to open the PDF file as an attachment or inline
Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Convert_Current_Page.pdf; size={0}", outPdfBuffer.Length.ToString()));
// Write the PDF document buffer to HTTP response
Response.BinaryWrite(outPdfBuffer);
// End the HTTP response and stop the current page processing
Response.End();
}
else
{
base.Render(writer);
}
}