I have an html file (which consists of both text and images) in my Documents library (it need not exactly be over there, what I mean to emphasize is - it's not in the internet and not included within the app package since it's dynamic content and created after the app has been deployed).
I want to display the contents of this page within a WebView. How would I go about doing that ?
You can (and should) copy your HTML Files to the LocalState Directory of your App which sits here: C:\Users\YourUSerName\AppData\Local\Packages\YourApp\LocalState.
You then can Access this with: ms-appdata:///local/
Very Basic example:
string url = "ms-appdata:///local/myWebpage.html";
webView.Navigate(new Uri(url));
You can find a lot of Information and samples in this sample download: https://code.msdn.microsoft.com/windowsapps/XAML-WebView-control-sample-58ad63f7
Related
I have 2 projects that are needing to talk to one another. The first is a ASP.NET MVC project, that when in production, has a feature where the user can edit an html template that is stored in the wwwroot folder of the project.
The second project is a C# console app that grabs some user data from a database, and then uses that data to email surveys to users. The html template from the first project is needing to be grabbed by this console app so that it can be used in sending out these emails. I was hoping to use HtmlAgilityPack to grab the html email template from the first project when it is live, something along the lines of this:
var web = new HtmlWeb();
var document = web.Load("www.sitename.com/EmailTemplate");
string text = document.ParsedText;
But I'm open to other ideas that might work in this case. More or less I think I just need to figure out how to access static html files from within the wwwroot folder from a browser path, if that's possible. Oh and these two projects are going to be running on different servers, so local paths won't work. Thank you!
In large part thanks to ADyson's comments, the course of action that makes most sense in this situation is to create a small API within the MVC app, that fetches the html file, and the console app will call this API to retrieve the needed html.
I had a similar problem, and I did it by adding ~/ in the beginning of my static file addresses in my _Layout.cshtml.
My template files and photos were no longer loaded in the project but the layout was loaded. This way the files were also loaded
I got an issu with the plugin IronPDF. I'm trying to render HTML and CSS display to a PDF files, it work in our web application, but now I'm trying to do the same stuff with a windows service. I tried to use Bootstrap locally to get CSS, that didn't work so I used CDN link and it worked. But now, I would like to use pictures, so I pasted a folder in the root of the project with pictures in it, I think it's an application root problem (Ibelieved that the application root was the root of the project, but it seems to be wrong)
I would like to have something like this:
https://prnt.sc/gvzvpg
but instead, I have something like that :
http://prntscr.com/gvzw1r
thanks for attention
I used before ItexSharp for generating PDF files with c#. For images, the path must be relative path. try it
I need to write the code which will load some HTML (received from external source by other means) into WebView and display images referenced in this HTML. These images will be stored locally somewhere in Windows.Storage.ApplicationData.Current.LocalFolder.
Neither of suggestions at use local image to display in webview for windows phone 8.1 or Use local images in Webbrowser control work for me. If I load such HTML with web.NavigateToString(html):
<html><head></head><body>A <img src="file:///c:/Users/idh/AppData/Local/Packages/d783af9f-88eb-42f5-ab0f-abb025f32baa_5cy2zb9g43kb2/LocalState/folder/image001.jpg"> B</body></html>
I get just A B displayed. I tried slashes and backslashes, double and triple slashes after file:, ms-appx: and relative paths instead of file:, etc. The image never gets displayed. At the same time, if I save this in HTML file and open it outside of my app, it's displayed fine. I also successfully save and read these files (they are actually appeared in that folder because my app created them so unauthorized access is not an issue). package.manifest does include Private networks.
I'd better not use embedding with base64, custom uri resolver and other special techniques because I'm not the actual developer of the application. I make a library which gets HTML and saves images to local storage and now I need to demonstrate an easy to use method to visualize the stored content in WebView like I earlier did for normal .NET framework. I.e. I'm actually writing a sample for developers who are users of my library and these folks will then use my sample to deal with this HTML and images.
As the last resort, I can end up writing base64 or custom resolver for UWP version of my sample (while for other platforms like normal .NET framework the same procedure is much easier). But I want to at least be sure that the direct route of selecting proper URLs for images in the source HTML is not possible and I won't end up with situation where I wrote some quite complicated stuff and then someone experienced in UWP apps reveals that I over-engineered things. I.e. some expert opinion "no, it's not possible in UWP and you MUST use base64 embedding or custom URI resolving" will work for me too.
Although NavigateToString supports content with references to external files such as CSS, scripts, images, and fonts. But it only supports content in the app package using the ms-appx-web scheme, and web content using the http and https URI schemes. It can't work with assets located in the apps local folder. So using file:///, ms-appdata:/// or ms-appx:/// scheme won't work here.
To achieve what you want, you can use Base64 encoded images or custom URI resolver. Also I think you can store the html string to a file under the same subfolder that stores these image assets. In the html, you can use relative URI to reference these assets like what in my previous answer. And then use the Navigate method with a Uri that uses the ms-appdata scheme. For example:
var html = "<html><head></head><body>A <img src=\"image001.jpg\"> B</body></html>";
var folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("folder", CreationCollisionOption.OpenIfExists);
var file = await folder.CreateFileAsync("html.html", CreationCollisionOption.OpenIfExists);
await FileIO.WriteTextAsync(file, html);
webView.Navigate(new Uri("ms-appdata:///local/folder/html.html"));
I have a simple Xamarin Forms project that has a WebView which is used to load an ASP.NET website on our intranet. Currently there is only a Android implementation in the solution.
The website being loaded includes several CSS and Javascript files. The CSS files are being linked via link rel tags, while the Javascript files are linked using script src tags.
I have put alert statements in a script on the page itself, as well as in the linked Javascript file. Using a browser on my computer, both alert statements show up, however in the WebView, only the alert for the page shows up.
I've also tried using the WebView.eval method to call a Javascript method in the linked file as well as one defined on the page itself. Calling the method defined on the page worked, but the one in the linked Javascript file didn't.
All that has lead me to the conclusion that for some reason the WebView isn't loading the Javascript files indicated by the script src tags.
From research I have done, there is mention of having to include the Javascript file itself in the Android and iOS projects, but those seemed to be for situations where the WebView source was being set to a constructed website, not pointing to an existing web site.
Here are samples of how the files are linked:
<link rel="stylesheet" href="/App_Themes/wms.min.css" />
<script src="/Scripts/wms.js"></script>
I have tried using absolute paths, which didn't work either:
<script src="http://path.to.site/Scripts/wms.js"></script>
What am I doing wrong? How do I get the the linked javascript files to load and be usable?
Edit: Update, apparently the file was being linked and loaded correctly, it was just being cached by the webview and or the android device or emulator.
Because of that, when I made changes to the javascript file, and tried to use / view those changes via the webview, it wasn't being reflected, since a new copy of the javascript file wasn't pulled from the website.
However, when I gave a new name to the javascript file and linked to that, the webview get the new file, and all my changes were there and it behaved as expected.
So, I will be using the clear cache method outlined here to clear my change when the app starts: http://www.tipsabc.com/2015/xamarin-froms-how-to-clear-webview-cached-files/
That way, every time it starts it will force the webview to get a fresh copy of all the files.
I am using an asp page where I want to have a hyperlink that, if clicked, will load an html page in the browser.\but as i user the asp:hyperlink and I am also giving the path of the html page where the page is stored. However, when clicked, it is not loading that html page..
i am using the hyperlink as:
<asp:HyperLink ID="hlinkTest" runat="server">Preview</asp:HyperLink>
and i am giving the url as
hlinkTest.NavigateUrl = "file:///E:/user/aspnet_app/source/test.html";
what can be the better solution for getting html page in browser..
I would be concerned that by trying to access a specific file location, you're opening yourself up for problems. You might have some real trouble porting the site to a new location.
If the target file is in the same folder as your hyperling source you could use:
hlinkText.NavigateUrl = "~/test.html";
Good luck.
Try:
Preview
Don't forget that the client browser will be unable to load a file: url unless a file with that name and path exists and the user has read access to it. So in the OP's original example, unless the machine running the browser that's trying to load file:///E:/user/aspnet_app/source/test.html has a readable file at E:\user\aspnet_app\source\test.html, they're get a big fat error along the lines of file or directory does not exist or can't be found. Try giving the user an http: URL, absolute (http://www.mysite.org/foo/bar/baz.html) or relative (foo/bar/baz.html). If relative, the path will be taken relative to the URL of the current page.