Open downloaded epub files in WebView - c#

I google a lot over this and still can't found a way to make it work
My app downloads epubs from many sources and then I save them to the local Storage (and unzip them)
I can read the extracted files with no problems with Storage commands but just can't to open it on WebView control
I tried all this:
The real path: C:\Data\Users\DefApps\APPDATA\Local\Packages\xxx\LocalState\***
Uri localUri = new Uri("ms-appx-web:///***/OEBPS/04_CL_CH.01.xhtml");
Uri localUri = new Uri("ms-appdata:///***/OEBPS/04_CL_CH.01.xhtml");
Uri localUri = new Uri("file:///***/OEBPS/04_CL_CH.01.xhtml");
Uri localUri = new Uri("file:///LocalState/***/OEBPS/04_CL_CH.01.xhtml");
Uri localUri = new Uri("file:///C://Data//Users//DefApps//APPDATA//Local//Packages//xxx//LocalState//***//OEBPS//04_CL_CH.01.xhtml");
// two ways
WebView1.Navigate(localUri);
WebView1.Source = localUri;
// this works, but as is an epub file, so need lot of files and styles
var XHTML = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("***\\OEBPS\\04_CL_CH.01.xhtml");
WebView1.NavigateToString(await Windows.Storage.FileIO.ReadTextAsync(XHTML));
This is a C# Windows Phone universal app, and I'm using VS 2013 Express

Take a look at http://blogs.windows.com/buildingapps/2013/07/17/whats-new-in-webview-in-windows-8-1/, it says:
ms-appdata:///local/TopLevelDirectory/file for files from the local
state
ms-appdata:///temp/TopLevelDirectory/file for files from the
app’s temporary state folder

Related

How to Open and Read text files on Android in C#

The code I have now is:
Intent intent = new Intent();
intent.SetType("text/plain");
intent.SetAction(Intent.ActionGetContent);
StartActivityForResult(Intent.CreateChooser(intent, "Select file"), 1);
string str = System.Environment.GetFolderPath(?????);
That last line of course doesn't work. I'd like the user to be able to browse folders on the device and open one of them. This is simple in Windows but Android is proving difficult.
1/3/2023:
This issue is still unsolved. Coming from Windows and C# this is all trivial. But Android, not.
I have tried these:
var documents = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
var documents = Path.Combine(Xamarin.Essentials.FileSystem.AppDataDirectory);
var documents = System.Environment.CurrentDirectorygetExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
WIth some I can save the file from my app on Android but I cannot find them (non-programmatically).
When I save files in FXTextEdit (on Android), I save them to folders I can easily access. Why not progammatically?
I am coding in C#.
Thanks.

C# UWP app crashes if SecondaryTile Imagesources are not from Assets folder

Basically I'm trying to make an UWP app to create Custom Tiles on Start Menu.
The SecondaryTile is working when the Tile Images are from Assets folder like this.
Uri square150x150Logo = new Uri("ms-appx:///Assets/square150x150Tile-sdk.png");
Uri wide310x150Logo = new Uri("ms-appx:///Assets/wide310x150Tile-sdk.png");
Uri square310x310Logo = new Uri("ms-appx:///Assets/square310x310Tile-sdk.png");
Uri square30x30Logo = new Uri("ms-appx:///Assets/square30x30Tile-sdk.png");
But I have images in ApplicationData folders and trying to use the path like this:
Uri square150x150Logo = new Uri(Path.Combine(ApplicationData.Current.TemporaryFolder.Path, "ImageSquare.png"));
Uri wide310x150Logo = new Uri(Path.Combine(ApplicationData.Current.TemporaryFolder.Path, "ImageWide.png"));
Uri square310x310Logo = new Uri(Path.Combine(ApplicationData.Current.TemporaryFolder.Path, "ImageLarge.png"));
Uri square30x30Logo = new Uri(Path.Combine(ApplicationData.Current.TemporaryFolder.Path, "ImageTiny.png"));
But this way the app is crashing. The debugger isn't working either. Any solutions?
To reference application data files, you should be using ms-appdata:///.
Calling TemporaryFolder.Path will return the absolute file path, C:\, which isn't allowed to be used on secondary tiles.
So your updated code would be...
Uri square150x150Logo = new Uri("ms-appdata:///temp/ImageSquare.png"));
OK finally figured it out.
In Addition to Andrew's answer, I had to change to using the LocalState folder instead of the TempState.
Andrew directed in the right way to use ms-appdata, but for some reason ms-appdata:///temp/ returned error. but using ms-appdata:///Local/ solved the Issue.

How to get all files list from a relative URI in XAML application?

How to get all files from a folder in XAML application using relative path?
I am playing with a Kinect application built in WPF. All images used in the application are in
[project dir]\Images\ and all backgrounds are in
[project dir]\Images\Backgrounds\.
I want to get list of all the images from Backgrounds directory using relative path. I have tried
DirectoryInfo(#"\Images\Backgrounds\").GetFiles();
but it says that Backgrounds directory must exist in [full path+project dir]\debug\bin\
Selecting each image manually works fine
Uri uri = new Uri(#"Images\Backgrounds\Background1.png", UriKind.Relative);
ImageSource imgsource = new BitmapImage(uri);
this.Backdrop.Source = imgsource;
It works for a single file because you specify URI to resource embedded in the assembly and not some folder on your drive, whilst GetFiles() will work only on a specific physical folder. So either you need to copy all your images instead of embedding them or use the code below and resourceNames should give you names of all resources that you can reference by URI in your project:
List<string> resourceNames = new List<string>();
var assembly = Assembly.GetExecutingAssembly();
var rm = new ResourceManager(assembly.GetName().Name + ".g", assembly);
try
{
var list = rm.GetResourceSet(CultureInfo.CurrentCulture, true, true);
foreach (DictionaryEntry item in list)
resourceNames.Add((string)item.Key);
}
finally
{
rm.ReleaseAllResources();
}
if you need then at this point each item.Value contains UnmanagedMemoryStream for each resource.
I would reply to your post instead of posting a solution, but I'm new to this site and dont have that privledge yet.... Hey! Just trying to help.
Anyway, I've had a problem similar to this before concerning DirectoryInfo. Can't remember exactly how I solved it, but I remember the backslashes being tricky. Have you checked out the MSDN site? It seems like it can't find your directory so its looking for it in debug by default. MSDN says the format should be "MyDir\MySubdir" in C#.

Can I reference local files in a webview when I use navigatetostring?

I'm using a webview to display certain data in my windows 8 app. I would like to user an include to a local js file as well as use locally stored images.
Is this possible?
I haven't had any luck by putting the local path where the files are located.
According to WebView documentation you can only reference other files using the ms-appx-web protocol, i.e. to load the files stored in Windows.ApplicationModel.Package.Current.InstalledLocation, meaning that they need to be distributed as content along with your application. The control doesn't support ms-appdata protocol for security reasons, i.e. you can't open files stored Windows.Storage.ApplicationData.Current.LocalFolder Windows.Storage.ApplicationData.Current.RemoteFolder or Windows.Storage.ApplicationData.Current.TempFolder where you'd need to put them if you were generating or downloading them at runtime.
In JavaScript apps WebView is a bit more flexible: it does support ms-appdata protocol as well, but only for media files such as images. It cannot open any potentially executable code, such as script or CSS.
If you want to open some local .html file or atc. you should download it in InstalledLocation folder. If you haven't option to create a new file you can just use file.CopyAsync(htmlFolder, fname + ".html");
For example I create some .html file:
StorageFolder htmlFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.CreateFolderAsync(#"HtmlFiles", CreationCollisionOption.GenerateUniqueName);
IStorageFile file = await htmlFolder .CreateFileAsync(fname + ".html", CreationCollisionOption.GenerateUniqueName);
and than I can easily open this .html file with or without FileOpenPicker:
var fop = new FileOpenPicker();
fop.FileTypeFilter.Add(".html");
var file = await fop.PickSingleFileAsync();
if (file != null)
{
string myPath = file.Path.Substring(file.Path.IndexOf("HtmlFiles"));
myWebview.Navigate(new Uri("ms-appx-web:///" + myPath));
}
And don't forget - just only from InstalledLocation you can open it with ms-appx-web:///
If the WebView is IE10 based, FIleReader may be what you are looking for. Here is a snippet of code that I use on an image ipload page to show images in a page when they are selected via a File Open dialog:
$('input:file').each(function(index, evt){
if(index===0)
{
var files = evt.files;
for(var i=0;i<files.length;i++)
{
if(files[i].name===filename)
{
var reader = new FileReader();
reader.onload=(function(theFile){
return function(e){
var line= uploader.fineUploader('getItemByFileId',id);
if(line!=undefined)
$(line).append('<img class="fileimage" id="fileImage-' + id + '" src="'+e.target.result+'" />');
};
})(files[i]);
reader.readAsDataURL(files[i]);
break;
}
}
}
I hope this points you in the right direction!

BitmapSource to point to local file

What's the best pattern to implement a local cache for a Metro Style App so that the images can be cached in the background while the app is being used on-line and to serve images when the App goes offline?
How do we set a BitmapSource to a local file upon discovering the lack of internet access? using new Uri(localpath, UriKind.Absolute) doesn't work.
Are the images already downloaded? If so, are they in the "Local" folder?
If so, you can build a BitmapImage from the path like this
var m_Image = new BitmapImage(new Uri("ms-appdata:///local/" + ImageFileName));
EDIT
If your file is stored in the package as a "never had access and can't download anything" standby, the Uri would be something like
var m_Image = new BitmapImage(new Uri("ms-appx:///Assets/" + FallBackImageFileName));

Categories