CefSharp Resources not loading when using LoadHTML - c#

I'm using CefSharp for showing a html file in CefSharp browser.
when I use web_view.Load(#"C:\htmlfile.htm"); it's show my body background.
but when i load htmlfile.htm and use web_view.LoadHtml(File.ReadAllText(#"C:\in.htm")); body background doesn't show?
I want to ask how do i must set address body background in html local file?
This is My Html File content:
<html>
<body background="C:\Untitled.png">
</body>
</html>

Try this:
web_view.LoadHtml(File.ReadAllText(#"C:\in.htm"), #"C:\in.htm");
The second parameters indicates the URL, if the URL is a local resource then, the page will be able to load local resources.

You will have to implement IRequestHandler.OnBeforeResourceLoad(), intercept each request, and read the bytes yourself from disk and suppy them to chromium as a response.
https://github.com/chillitom/CefSharp/blob/master/CefSharp/IRequestHandler.h#L26

Another way you could do this is use a schemeHandler (it's cleaner IMO).
Register a scheme with CEF -
CEF.RegisterScheme("ascheme", new HandlerFactory());
Add a scheme handler that loads the PNG from disk and returns a relevant response
then change your html to refer to the scheme:
body background="myscheme://Untitled.png"
I can fill in the blanks if need be... but that should be enough!

Related

Displaying images from database using c# .net

recently, I've been developing a website with .NET c# and I've been trying to display images via the imagepath in the db. It is currently working by this line of code.
return File(byte array, "image/jpeg");
The problem is that by this line of code the layout page is completely ignored as only the image with white background is shown. I need to display the image along with the return view (the image inside the layout of the website). thx
The reason you're not seeing anything is because you can only have one type of response per request; Either a response containing HTML or a response containing the image.
What you need to do in order to serve images via your application is set up a route that you can put in your <img> tags as the src attribute.
<img src="/static/images/123">
Your route would listen for requests on the /static/images/ path and then try to parse the ID number at the end. It could then take that ID number (123) and look up the relevant image in your database.
So, to be clear, you'd have at least two requests that occur; First, you serve the request for the page, then you serve subsequent requests for the image(s). These two request handlers do not share the same code.
Finally, if you really wanted to "inline" an image as part of the page response, The only way you can "inline" an image into a page is to base64 encode it and set that as as the src of an <img> tag. This process is slow and bloats your HTML, making it take longer to load.

I want to hide URL in the addressbar

I want to display the report(pdf) which was available in the webpath assume (http://myservre/reports/myrepoft.pdf). Currently, It was being displayed via IFrame like this.
strReportPDFFile = "`http://myservre/reports/myrepoft.pdf`"
<IFRAME frameborder="1" onload="onchangestate()" src="" id="ifrmShowReport" frameborder="0" height="100%" width="100%" marginwidth="0" align="top" style="overflow: none;overflow-x: auto;display:none"></IFRAME>
document.getElementById("formContainer").style.display = "block";
document.getElementById("ifrmShowReport").style.display = "block";
document.getElementById("ifrmShowReport").src = strReportPDFFile;
The URL was displaying in the address bar which I want to hide URL. Is it possible ? If not, please suggest some better alternative.
I argue that the main reason of your question (hiding the URL of the PDF file) is for security issues.
E.g. you have some PDF files on your server that have to be sent and read only from intended audience and not from everybody.
I give you some basic hints on how to realize this intent:
place the PDF files out of the public root of your webserver
realize a server side script that:
check is the user is authenticated and have the grant to get the file
get some parmeters to retrieve the correct file
serve the file with this server side script content-disposition (as attachment, sending the correct Mime-Type and a "ficticious" URL)
Since you put C# in your question tags i address you on
Add a content-disposition to the header with C#:
Response.AddHeader("content-disposition", #"attachment;filename=""MyFile.pdf""");
Then you can display your pdf wherever u wish: blank page, iframe ...
It is sure you'll not find a solution just working with HTML/Javascript, not one of those easly to avoid for a medium level internet user.

Display varbinary as a downloadable link

Fairly straightforward question, I have images stored in my database as varbinary and would like to provide a link to these images rather than displaying them on the website. When a user clicks the link he/she should be able to download the image.
An image is served in response to a request.
<img src="?" /> <!-- what goes here? -->
You need to create an HTTP handler to receive requests for these images.
A handler is an executable available at a specific URL which can respond to your request; in this case, by serving the binary data of an image. An ASPX page is a valid handler, though there are more efficient handler types for images.
<img src="MyHandler.aspx?imageId=123" />
The handler should do a few things:
validate that the ID is valid and that the caller has permissions (if needed)
retrieve the image from the database
set appropriate response headers
use Response.BinaryWrite() to send the binar data to the client.
Note that if you are using ASP.Net MVC, you can use a controller as your handler.
An alternative method is to base 64 encode the bytes and embed them directly in the image tag.
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">
This is a useful technique for small images or when the expense of multiple requests is very high.
See: http://en.wikipedia.org/wiki/Data_URI_scheme
More reading:
Dynamically Rendering asp:Image from BLOB entry in ASP.NET
Dynamic Image Generation
You should create a page that takes an image-id and returns the image.
You may consider this question:
Display image from a datatable in asp:image in code-behind
I had a similar problem an that answer solved my question. Beside that, you can use binaryimage component from Telerik:
http://www.telerik.com/products/aspnet-ajax/binaryimage.aspx

drawing lines on screen?

I've been reading through a few asp.net articles, and attempting some code, but I think I may be confused. Can you or can you not draw lines on the screen with code on a ASP.NET webform using c#?
If so, can anyone direct me to some examples?
You cannot directly draw on a webform. You may draw on the image and then embed it on your webform (like any other image).
You can make a canvas and then you can draw whatever you want to draw on it.But direct drawing is impossible.
If you are fine with HTML5 you can try lineto Javascript method:
<script>
context.lineTo(100, 200);
</script>
Please refer # following link for more details:
http://www.html5canvastutorials.com/tutorials/html5-canvas-lines/
Not sure what you mean by "draw a line" but if you are using a web browser you need some kind of HTML object to display this "line". If all you need is a horizontal line you can just add a HR html tag and use CSS toy stylize it. You can also include this line in an image or HTML5 Canvas.
Because there is not screen for your server side code. Your code generates HTML, JavaScript etc. and then browser uses that content to render it on the client screen. So your options are to generate image (draw all that you need) in server side code and send it to the browser, or you can use JavaScript and send browser instructions how to draw lines.
You could probably create a new image using the System.Drawing namespace classes and then do something like dynamically load it into an <img /> tag...but depending on what you're trying to accomplish it may be far easier to either use a JavaScript library of some sort or to use some sort of very simple line image and tweak the length/height using css.
More detail would be needed to understand what you're trying to do. As others have pointed out though, there's no direct way for your C# code to interact with the page. You'd have to have something on the page like an img tag and then set it's source to a C# file like a handler (.ashx). In that handler you could generate the image and then set the response content type as image/jpg and write the raw bytes to the response stream...
Again though, that seems like overkill for something that could be accomplished with CSS or javascript.

Show the server-side generated HTML in a new window

What is the best way to show the server-side generated HTML (full page) into a new popup window? It should be triggered upon clicking a button (causing a postback to the server).
Thanks
Edited:
The HTML content are dynamically generated in the code behind and the content is full page (<html> ... </html>). Upon clicking a button on the web page, I would like to get the generated html content and pass it to the browser and show it in a new popup window. The content will be the final result (UI) not HTML tags.
You can send the same page with mime type text/plain
For instance with a
<a href="same url?mime=textonly" target="_blank">
On the asp server, when the argument mime=textonly is detected, you change the mime type to text/plain
Perhaps I should have started with a comment to get more information but can you not:
Post back to a new window on click? <a target="_blank">
Though if the requirement is for the server to generate the new window, just append something like:
<script>window.open('title'); </script> at the end of the response and have the server populate that.
You could probably have the server code save the HTML to a file and output <script>window.open('urltothefile');</script>. Just make sure that you write a unique filename each time.
Alternatively, you could have the server code store all related information into a database and output <script>window.open('showResult.aspx?id=123');</script>, where 123 is the id of the database record. Then in showResult.aspx, have it generate the required HTML.
Another option is to output the HTML into a div with style="display: none", then have some javascript to assign the innerHTML to the newly opened window. eg:
var w = window.open ('_blank');
w.document.body.innerHTML = document.getElementById("returnedHTML").innerHTML
One more possibility is to have a WebMethod. I don't really remember how to declare one, but it is a server function that can be called from the client. Open the window via javascript, call the webmethod and place the result as the innerHTML of the newly opened window; Pretty much like the previous option.
All these are good but they don't work when you use UpdatePanel and partial rendering. If that's the case, it's a whole different story.

Categories

Resources