How to store variables on local HTML file? - html

Recently I've found that I can use pure HTML, CSS and JS to build Android app (and iOS also maybe), by using PhoneGap. I guess it converts HTML pages to Android web views. So basically I'll have ~10 HTML files, calling each other, get data from Server (it's Java and Tomcat) via Ajax and JSONP.
And my problem is about storing user data with these static local HTML files.
As they are local files, I can't use Cookie. But somehow Session's still working, so with each HTML page, I can set an onload event that sends an Ajax request, then get data. But it's very inconvenient, I have to do that for the same data each time I switch to another HTML file. So, is there any solution I can load all data when the first HTML file is loaded, store data somewhere, then reuse these data in others HTML files ? Thank you so much for reading my question.

Related

Is there a good alternative for embedding a PDF with HTML next to using a local file path, online file path or data source as base64-string?

I am building a web app and I would like to show PDF files to my users. My files are mainly stored as byte arrays in the database as they are generated in the backend. I am using the embed element and have found three ways to display a PDF:
Local file path in src attribute: Works, but I need to generate a file from the database byte array, which is not desirable as I have to manage routines to delete them once they are not needed anymore.
Online file path in src attribute: Not possible since my files may not be hosted anywhere but on the server. Also has the same issues as the previous method anyway.
Data as base64 string in src attribute: Current method, but I ran into a problem for larger files (>2MB). Edge and Chrome will not display a PDF when I covert a PDF of this size to a base64 string (no error but the docs reveal that there is a limit for the data in the src attribute). It works on Firefox but I cannot have my users be restricted to Firefox.
Is there any other way to transmit valid PDF data from a byte array out of the database without generating a file locally?
You have made the common mistake of thinking of URLs and file paths as the same thing; but a URL is just a string that's sent to the server, and some content is sent back. Just as you wouldn't save an HTML file to disk for every dynamic page on the site, you don't have to write to the file system to display a dynamic PDF.
So the solution to this is to have a script on your server that takes the identifier of a PDF in your system, maybe does some access checking, and outputs it to the browser.
For example, if you were using PHP, you might write the HTML with <embed src="/loadpdf.php?id=42"> and then in loadpdf.php would write something like this:
$pdfContent = load_pdf_from_database((int)$_GET['id']);
header('Content-Type: application/pdf');
echo $pdfContent;
Loading /loadpdf.php?id=42 directly in the browser would then render the PDF just the same as if it was a "real" file, and embedding it should work the same way too.

How to print a file to an html page in django

I have a Django application where the logging is being printed to a file using the logging module.
I want to be able to have the contents of the log file also display on an html page such if a new line gets printed to the log page, the html page will also actively update to display the new line without having to be refreshed.
How can that be accomplished?
You need JavaScript for updating HTML without refreshing the page.
Create or edit the HTML with JavaScript and use AJAX for fetching latest logs every x seconds or how often you want the page to update. AJAX calls some Django view that returns the log data. For example, the view could read 10 latest lines from a log file and return that. JSON is probably the easiest format to work with but any format is fine (doesn't have to be XML like in the picture).
Tutorial for doing something like this with Django + jQuery
Depending on how complicated the logging page is, you may want to check out Django's documentation for JavaScript, storing JavaScript files in the project if JS code is not just in the HTML files, Django Rest Framework and different JavaScript libraries/frameworks such as React or Vue.

Node.js/Express.js - How to render a remote HTML file to the client?

I am currently in the process of making a blog website, writers for this website have the ability to upload AMP HTML files and the assets required for does files to work. Both the html file and their assets get sent to our CDN.
Now, when a client visits the website in the link example.com/13215 the server gets the parameter 13215 and checks what post it refers to in the database and retrieves the link to the HTML file. How can I send this HTML file to the front end with Node.js/Express.js even though it is a remote file.
Just copy pasting the URL into response.sendFile() and response.render() functions just throws errors. I thought about reading the file contents then writing them to a file then sending them to the client but I don't think that's a good idea performance wise.
Is there a way to achieve this?

Can node-red html be edite elsewhere?

I'm developing a node-red application right now that uses a html response. The html uses google maps, visual indicators and websockets. It is very hard to debug this system through node-red's little html editor. Is there a way to edit the html file through any normal editor (e.g. vs code) and then deploy the application again to see the effect ?
One solution that came to my mind was to read from an external file using the file node and return it as html, put I don't know if that works. Is there a better way ?
You can create and edit static resources (html/css files etc) however you'd like and then serve them from Node-RED.
You have two options for serving static content:
create corresponding HTTP In -> File In -> HTTP Response flows for each file you want to serve
or use the httpStatic property in your settings.js file to identify a directory whose content should be automatically served by the runtime.

HTML 5 - load text from text files

I am facing problem in HTML 5. I need to statically load data into web page from local saved files. Up to now, I have been only able to load data via < input type="file" id="fileinput" / > but I want to load data from static location, which never changes. How to do that? And is there any way how to determine, whether some local file was changed from previous version?
Thanks
no, this isn't possible if by 'local', you mean a file at /home/waypoint/somefile.txt. You can make a 'link' with the filesystem api (if you selected it in an input field, for instance), which is valid to do computations with it (to read it, write to it, display it in img,etc). But it is deleted/unvalid, as soon as the window closes. If you could just magically "read" any local file via javascript which resides on the file system, who would stop google to read out your /etc/passwd file?
if your local computer is also your server and therefor your server-side code has access to the local file /home/waypoint/somefile.txt, your app can get it via ajax. Checking if the file exists, would be done the same way.