How to clear cache of IFrame using c# Code in asp.net - c#

In my asp.net (Runtime Version 3.5) there is a requirement of displaying Pdf in the middle of the page along with other content.
So in such case I found the solution. for this I used Iframe . But we have to display the pdf on linbutton_click event.
HTML part
<div id='Pdfviewer'>
<iframe id="ifrDisplay" runat="server" scrolling="auto" width="600" height="700"></iframe>
</div>
C# code part
LinkButton1_click
{
string pdfUrl = "https://test-folder.com/rxdata//P09/201305240039MA9_000/Data/00000010.pdf";//
ifrDisplay.Attributes.Add("src", pdfUrl);
}
Here the issue is even if the content of the particular pdf changes, still it loads the pdf of older content.
So it is clear this is caching related issue.
Please let me know how to remove the cache due to which still the older content of pdf file is loading.
--Sarthak

To avoid caching you can use some random number in the URL so that browser does not cache the response.
string pdfUrl = "https://test-folder.com/rxdata//P09/201305240039MA9_000/Data/00000010.pdf"+DateTime.Now();
The second possible option is that who is serving PDF should set the cache to (-1) so that it not get cached in the browser.

Related

How do i load html from a db and display images that are stored locally in the project

I am storing the content for my page in a sql server database. When i load the content onto the page i have a tag:
<img src="~/Images/Course/html.jpg" />
The html is being rendered using #Html.Raw(content).
when the page loads i get the error:
GET http://localhost:51249/Course/Index/~/Images/Course/html.jpg 404 (Not Found)
I dont know why it does this. Is there any way to have images stored in files within the project, then when loading the html from the db the local linking works? Do I have to host the images somewhere and link to them via https? Any help would be great.
Fixed it - just remove the tilda from the value in the database. Here's a small sample code in the HTML to reproduce the same problem:
<img src="~/images/house.jpg" />
#{
var something = "<img src='/images/house.jpg' />";
}
#Html.Raw(something)
The first line displays the image direct from the location, using the tilda, which .net resolves to the root of your project.
The second line doesn't use the tilda, which automatically comes from the root of the project, but leaving the tilda in messes up.
Fix: delete one character from the HTML stored in the database.

Google docs embedded document not showing until resize?

I have a problem in which sometimes (not clear when exactly), displaying an embedded document with docs.google.com does not show the document until I resize the browser window.
Grab some popcorn, here's a movie that shows the problem:
Here's my code:
HTML:
<iframe runat="server" id="frame" style="width:100%;height:600px;border:0;"></iframe>
C# Code begind (in Page_Load method):
frame.Attributes["src"] = "https://docs.google.com/gview?url=https://example.com/1.doc&embedded=true";
I tried reproducing it by browsing directly to https://docs.google.com/gview?url=https://example.com/1.doc&embedded=true but it never happens this way.
Note: Sometimes the document is never displayd at all, as if it was never loaded, and the iFrame remains totaly blank.
Any ideas how to fix this?
You could try to setup a default document size. This is what I do to print envelopes from Google Docs
var envelope10={};
envelope10[DocumentApp.Attribute.PAGE_HEIGHT]=296;
envelope10[DocumentApp.Attribute.PAGE_WIDTH]=684;
envelope10[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
envelope10[DocumentApp.Attribute.FONT_SIZE] = 18;
envelope10[DocumentApp.Attribute.BOLD] = true;
envelope10[DocumentApp.Attribute.LINE_SPACING]=1;

Including Large Static HTML File with Image in MVC View

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/

Most efficient way to load a large buffer into an html element

I have made a custom report viewer control that utilized the output of an ssrs render request. I am having difficulty matching the speed of the ssrs ReportViewer control. The report is 2.5 mb.
I am watching the request and responses over the wire and can see it is taking roughly 2.8 seconds to get the buffer of the contents from ssrs. However, it is taking 10-15 seconds to show the content inside of an html element in my current view.
The problem is when I log into the ssrs manager directly and render the report it takes just as long to process the report however, it only takes, 3 seconds to render in the ssrs report viewer control.
The original design was to place the html as a payload on the ajax response. Then I thought that the json serialization/deserialization could be the bottleneck so I added the ability to save the output to a temp folder on the server as html and call load(html)...no improvement.
I am using the code below to load the content in my page from the Ajax.BeginForm success function :
function setReportContent(content, isUrl) {
if(isUrl)
$('#reportContent').load(content);
else
$("#reportContent").html(content);
showReportWaitIndicator(false);
$("#reportContent").show();
}
I cant figure out why there is such a huge discrepancy in rendering times between the ssrs report viewer control and the way I am loading.
Is there a more efficient technique to load large content other than .load(url); ?
Edit: I have downloaded the html file and opened it directly in the browser and the markup was rendered instantly. Why does appending the contents to the dom take up to 20 seconds?
Thanks
I found one solution but it uses an IFrame and I am iffy on that but it matches the speed of the ssrs report viewer. In a nutshell loading an IFrame does not contain some of the overhead of placing the contents in the dom. Made a small adjustment to the setip and it works.
function setReportContent(content, isUrl, renderInIFrame) {
if (isUrl) {
if (renderInIFrame)
$("#reportContent").html("<iframe id='reportFrame' scrolling='yes' onload='resizeReportFrame();' src='" + content + "'\></iframe>");
else
$("#reportContent").load(content);
}
else {
if (renderInIFrame) {
$("#reportContent").html("<iframe id='reportFrame' scrolling='yes' onload='resizeReportFrame();'\></iframe>");
$('#reportFrame').contents().find('html').html(content);
}
else
$("#reportContent").html(content);
}
showReportWaitIndicator(false);
$("#reportContent").show();
}

C# Getting Text from object in web page

I'm trying to recreate an old C# application of mine that streams from an online radio station. Problem with the old one is, it loaded an entire web page just to display a certain area of it, which takes more resources that I would deem necessary. So, now I'm rewriting the entire application, and am looking for a way how I can retrieve text from the following code on the website:
<div id="now" style="visibility: visible; display: block;">
<div class="scroll" style="margin-left: 0.000px;">
<div id="title">SONG_NAME</div>
<div id="artist">SONG_ARTIST</div>
</div>
</div>
This piece is constantly updated on the page, with the name and artist of the current song.
id="title" is the name of the song and id="artist" is the artist of the song.
I would like to retrieve the name and artist every say, 10 seconds or so.
Any idea what code to use for this ?
You'll probably want to pull the entire page back. The main considerations are:
You could request the html as uncompressed and open the stream using HttpWebResponse.GetResponseStream and then read up until the end of the block you need (you'll need to analyse the text as you go), and finally call HttpWebResponse.Close to close the stream and release the connection
If the entire response is compressed it may be more efficient to get the whole thing anyway before decompressing.
You need to test which is more efficient for the specific page you are scraping.
So the usual way is to retrieve the whole html stream, then use regex to find the block you need, and just keep your code simple.
Recommendation
If you want to keep it really simple then look at HtmlAgilityPack, which is even on NuGet to use with Visual Studio 2012. It makes working with html scraping very simple.

Categories