Value of input is from a file - html

<input id="LoadText" value= 'data.txt' />
I am trying to give the default value of the LoadText input to the contents of the 'data.txt' file.

Note: This is a solution, if the file data.txt is stored on the local file system of the client, if it is stored on the server, please have a look at Daniel's solution...
To read a local file into your site there a mainly two possible solutions:
Uploading the file to the server and download it again with AJAX
Use the File API. The current support for it can be seen on this site: http://caniuse.com/fileapi
If you store your HTML file also on your local file system (and you never want it to be stored on a server), you can also directly use AJAX. Here is a code using jQuery:
$.ajax({
url: "data.txt",
dataType: "text",
success: function (data) {
// do something with the text of data.txt (which is stored in the value data)
}
});
The files data.txt and your HTML file must be stored in the same directory for this.

You need to use a dynamic programming langauge to achieve this behaviour. You could use a serverside technology like PHP to read the content of data.txt and insert its content into the html before sending the file to the client. Or you can use clientside technology like Javascript to load the data.txt file speratly and insert it into the input form afterwards.
Update PHP example:
If your webspace/server/etc supports PHP you could simply write something like this:
<input id="LoadText" value= '<?php echo file_get_contents('data.txt'); ?>' />
But you need to take care of the content of data.txt . The file could contain anything and therefore break your design. Just imagine the following content:
' /><any tag and code you do not want in your design

Related

SAS input json into HTML

Im creating an HTML file from SAS like the following
data _null_;
file './test.html';
put '<DOCTYPE html>';
put '<html>';
put '<script>'
put '</script>'
put '</html>'
The problem is that I need to take a SAS dataset, convert it to JSON format and insert it into the HTML file. The pseudo code is:
data _null_;
file './test.html';
put '<DOCTYPE html>';
put '<html>';
put '<script>';
sasDataFrame -> to Json
put 'console.log(sasDataFrame)';
put '</script>'
put '</html>'
I know that proc JSON allows me to convert SAS dataset to Json, but i don't know how to embed the string result into an HTML through this sort of put statement.
Anyone knows how to accomplish this?
Create the JSON file as a text file using PROC JSON or whatever method you want. Then write the contents of that file into your HTML file.
So let's assume you have pointed the fileref JSON to file with the JSON text in it.
Now first write the header information, then the contents of the file, then the trailing information.
With your simple example you could do it all in one data step. Just read the lines from the JSON text file and write them to the HTML file you are creating.
data _null_;
file './test.html';
if _n_=1 then put
'<DOCTYPE html>'
/ '<html>'
/ '<script>'
;
if eof then put
'</script>'
/ '</html>'
;
infile json end=eof;
input;
put _infile_;
run;
For more complex file generation you can use multiple data steps to construct the HTML file. Just use MOD option on the FILE statement to append text to an existing file instead of making a new file.
Streaming web content like this is an antipattern!!
Rather than fiddle around with put statements, why not serve your index.html through the web server, and send the data from SAS directly as a pure JSON payload?
Here's a repo we created that will let you talk to SAS from frontend: https://github.com/sasjs/adapter
And here's another one with a ready-made web app you can use as a starting point: https://github.com/sasjs/minimal-seed-app
Whilst plugging SASjs (an open source tool my team created) I'll also point out that by using this approach you can easily deploy and run your web app on Viya and Base SAS as well as SAS 9 BI. It'll also run much faster and be easier to maintain.

Write Full HTML Elements in JSON and onto screen [duplicate]

So what I'm trying to do is get text from a file in the same directory as my html file using JavaScript. I want to store an array inside a text file and change it whenever i want instead of constantly having to go into the code, save it, check if it works etc.
I've tried looking around but couldn't find any clear information, most of what I found is using .readAsBinaryString, etc..
I'm mostly seeing things like this but i can't seem find anything which is actually getting information from a textfile without making the person find the text file directory.
function storearray(newval){
var file = "file location;"
var txt = file.txt;
var array = txt.split("|");
txt = txt + newval + " | ";
return array;
}
To read a file from the user's disk you need to use FileReader and the user must explicitly select the file using a file input. (See JavaScript read file without using input).
To read a a file from the website you need to use Ajax (with fetch, XMLHttpRequest or a library that wraps around them like Axios). (See Using fetch from MDN).
If (as it seems here) you want to read data from the website but the website exists only on the user's disk then you still need to use Ajax but will usually run into security restrictions. Some browsers allow you to disable the security protection, but the general solution is to install a web server and load both HTML and the data file using HTTP.
Alternatively, you can store your data in JavaScript (you are generating an array from your text file, you can so that manually or have a build-time script do it) and just load it with a <script> element.

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.

read and write a text file locally using chromium

I am running a web application from a local folder using Chromium.
Let's say I have my application contained in the following folder:
C:/Myfolder/App
I also know how to read a text file using the HTML5 APIs using a
<input type="file" name="files" id="fileElem" />
now I would like to know if I can read the file without the user having to select the file I am trying to read. The file is always contained in the following folder:
C:/Myfolder/App/Files
a subfolder of the folder containing the HTML that defines my application so there will not be any security restrictions and the file is always run through chromium locally. The file I am trying to read is a text file. Can I also modify and write the file in the same location? If yes would you be so kind to provide some example code?
Thank you very much,
Set TChromium.Options.FileAccessFromFileUrls to STATE_ENABLED and then you can use your normal AJAX functions on file://-Urls.
If you are using jQuery, you could use this code:
$.get('file:///c:/Myfolder/App/files/content.xml',function(data){
console.log(data);
})

How to upload file by HTML only

<form action="upload.html" method="post" enctype="multipart/form-data">
<input type="file" />
<input type="submit" value="Upload" />
</form>
I saved this file as main.html
I made a dir called 'images' with chmod 777
what should be in the file called 'upload.html' ??
I don't want to use any php or asp files
just html files, so how can I do it by html files only ?
Client Side (Browser) => Web Server => Script => Server Manipulation
You CANNOT just use HTML files to upload to the server.
Web servers do serve static content (think 'GET') however
when it comes todynamic webpages or the ability for the client (browser)
to POST/PATCH content on the server you would need some sort
of script for that!
Choose perl, php, ruby, python, javascript, or whatever makes
you smile, and put that behind your webserver, with the appropriate
image uploading logic and you're good to upload pictures directly
to your server then :)
It's simple. You can't.
Call, main.php and have that contain code shown here to upload files to your server. It is fairly simple to do.
Your still going to need to use PHP/ASP somewhere. It's easier to utilize a single PHP file, but if you wanted your end users just to see an .html file instead, you could have it post to a php script file (but it must be a .php extension for the server to execute it).
As mentioned earlier, have a look at the W3 Schools post on PHP file uploading.
The simplest answer is no , you cant do it with the help of html . You need to use php or any server side language to handle file upload .
See this link : W3schools php file upload
Or you can use any other server side languages like asp , python , jsp etc...
An another possiblility is you can use javascript (client side) . Use javascript with ajax ..
See this link Click here