Load values of a file at an html form - html

Is there any way to load the values of .txt file or .doc file and insert them at an html form as possible values? For example i have rooms 1-10 written at a txt file, and a form that i will need to scroll and check anyone of that rooms. Thanks

In the past I did the following: one iFrame loads the txt file, with the visibility: hidden;position: absolute style. Its onLoad event triggers a Javascript function which can parse it. I usually use this reading function:
iframeObj = document.getElementById('put here the iFrame ID')
if (iframeObj != null)
{
if (iframeObj.contentDocument)
{
txtfilecontent=iframeObj.contentDocument.getElementsByTagName("BODY")[0].innerHTML;
}
else if (iframeObj.contentWindow)
{
txtfilecontent=iframeObj.contentWindow.document.body.innerHTML;
}
// Here you can use the txtfilecontent, which is a string
}
Usually I trigger a reload of the txt file in the end, but if you have to fill a form it is not recommended.
If you want to do that before loading the page, however, you should use a server side code, such as PHP and its file_get_contents function. The advantage of this approach is that you won't need to expose the txt file to the user, but it can stay hidden on the server.

Related

(Delphi) Is it possible to change a label to whats in a .html file?

Is it possible to change a Label caption to what is in a .html file? Lets say the .html file contains 1.0 in it. Can I make the Label retrieve that and change its Caption to that value?
Eg. lbl1.caption := http://www.example.com/example.html;
Is it possible to do this?
TLabel cannot load content for you, whether that is from a file or a remote URL. You have to write your own code to retrieve the content yourself, and then you can assign it to the TLabel. For example:
// using the Indy TIdHTTP component...
lbl1.Caption := IdHTTP1.Get('http://www.example.com/example.html');

Downloading multiple files with a data URI

I know how to setup a href and a download attribute on an anchor to allow a user to download data as a file.
However, a client has requested that one link download two files together AND not be zipped! Don't ask me why!
Looking online I found the following solutions:
1) create 2 iframes on the fly and in each's form set its GET to the location of one of the files on the server, then run a form submit...here
2) A variation of (1) using a JQuery plugin..here
3) Opening popup windows. (not worth the link)
I'm wondering if I can handle this on the JS side? In the same App, I'm exporting data to CSV with the following code:
$elm.attr('href', 'data:text/csv;charset=utf8,' + encodeURIComponent(_str)).attr('download', fileName);
Where _str is a flattened two dimensional array.
Can I somehow trail or attach a second file to that?
You can create links on the fly and fire navite click event.
function downloadAll(files){
if(files.length == 0) return;
file = files.pop();
var theAnchor = $('<a />')
.attr('href', file[1])
.attr('download',file[0]);
theAnchor[0].click();
theAnchor.remove();
downloadAll(files);
}
$('a.download-csv').on('click', function(){
downloadAll([
['file1.csv', 'data:text/csv;charset=utf8,'+
encodeURIComponent('my,csv,file\and,so,on')],
['file2.csv', 'data:text/csv;charset=utf8,'+
encodeURIComponent('my,csv,file\and,so,on')],
['file3.csv', 'data:text/csv;charset=utf8,'+
encodeURIComponent('my,csv,file\and,so,on')]
]);
});
Here you can find the fiddle with a working demo.
Here is the article on my web site with all the details

LOCAL HTML file to generate a text file

I am trying to generate a TEXT/XML file from a LOCAL HTML file. I know there are a lot of answers to generating a file locally, usually suggesting using ActiveX object or HTML 5.
I'm guessing there is a way to make it work on all browsers (in the end HTML extension is opened by a browser even if it is a LOCAL file) and easily since this is a LOCAL file put in by user himself.
My HTML file will be on client's local machine not accessed via HTTP.
It is basically just a form written in HTML that upon "SAVE" command should be generating an XML file in the local disk (anywhere user decides) and saving form's content in.
Any good way?
One way that I can think of is, the html form elements can be set into class variables and then using the jaxb context you can create an XML file out of it.
Useful Link: http://www.vogella.com/tutorials/JAXB/article.html
What you can do is use base64 data-urls (no support for IE9-) to download the file:
First you need to create a temporary iframe element for your file to download in:
var ifrm = document.createElement('iframe');
ifrm.style.display = 'none';
document.body.appendChild(ifrm);
Then you need to define what you want the contents of the file to download to be, and convert it to a base64 data-url:
var html = '<!DOCTYPE html><html><head><title>Foo</title></head><body>Hello World</body></html>';
htmlurl = btoa(html);
and set it as source for the iframe
ifrm.src = 'data:text/x-html;base64,'+htmlurl;

Select, Copy from IE and paste to Excel

So the basic question is, how can I make table nicely copy pastable from IE to Excel or OpenOffice calc.
The page is located here: http://tuudik.lohv.eu/Asjad/EURXML/
The table is created dynamically by PHP code. There's a button with tekst "Vali tabel!" which means, it selects the table. Then I want to copy it, for example using CTRL+C and paste it into a spreadsheet.
What are the best options? Should I create a hidden div what then has the same Data like in the table but formatted other way?
Also, anyone knows how to make the "Vali kõik!" button work so, that it would automatically copy the contents to the clipboard?
Why not modify your PHP script to write the page content directly to Excel, writing the content to excel cells rather than HTML where you're writing to the
<td> elements, then set the headers to download it... so passing an additional 'format' parameter to your script could determine the output format as HTML of XLS. The PHPExcel library offers the capability of generating XLS files in pure PHP. A link on your HTML page could then offer the option to recreate the output directly as an XLS file for download, rather than copy-and-paste.
In my experiences copying from html pages is best done through a text editor that doesn't have formatting. This way the content is usually delimited in some kind (tab usually) that makes pasting to a spreadsheet much easier.
Copy table from browser
Paste in notepad, select, copy again
Paste in Spreadsheet application
Might not answer your question directly but if you somehow can make the data copyable as plain text with tab delimiter (as in a hidden div or something) - you would probably have greater success in pasting it into spreadsheets that supports all kinds of formatting.
Using a slightly different approach, try the Get External Data feature in excel
( i tested on Using Excel 2010, but 2007 is similar, and it is available in 2003 in a slightly different guise )
From menu Data/Get External Data/From Web
opens a form:
paste address;(eg http://bit.ly/Kurss) and click Go
web page is displayed
scroll down to the table you want
select it with the little yellow arrow thingy
Click Import
Voila, data is in excel, as unformatted data from a background query!
Then whenever you waht to update the data, just click Refresh
I to needed a way to copy html table information and paste into excel. I used a combination of techniques. First I added this code so I could automatically copy data into the paste buffer of the clients pc.
How do I copy to the clipboard in JavaScript?
// Copies a string to the clipboard. Must be called from within an
// event handler such as click. May return false if it failed, but
// this is not always possible. Browser support for Chrome 43+,
// Firefox 42+, Safari 10+, Edge and IE 10+.
// IE: The clipboard feature may be disabled by an administrator. By
// default a prompt is shown the first time the clipboard is
// used (per session).
function copyToClipboard(text) {
if (window.clipboardData && window.clipboardData.setData) {
// IE specific code path to prevent textarea being shown while dialog is visible.
return clipboardData.setData("Text", text);
} else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
var textarea = document.createElement("textarea");
textarea.textContent = text;
textarea.style.position = "fixed"; // Prevent scrolling to bottom of page in MS Edge.
document.body.appendChild(textarea);
textarea.select();
try {
return document.execCommand("copy"); // Security exception may be thrown by some browsers.
} catch (ex) {
console.warn("Copy to clipboard failed.", ex);
return false;
} finally {
document.body.removeChild(textarea);
}
}
}
I then created a copy string. Separate the cell values by a tab \t and separate the rows by a new line \n. see the example below.
I create the copy data string using PHP.
//-- Create data for a single table row
//-- We needed to escape single quotes for HTML and javascript
$copy_data = str_replace("'","\'",$col1."\t".$col2."\t".$col3);
//-- Create data for a multi rows
//-- Separate multiple rows by \n but needs to be escaped so use \\n
$copy_data = str_replace("'","\'",$col1."\t".$col2."\t".$col3."\\n".$col1."\t".$col2."\t".$col3."\\n".$col1."\t".$col2."\t".$col3);
Then I place it in a copy button on the web page.
Copy
Then you click on the "Copy" link to get the data into your copy buffer. Next go to Excel and click in the cell where you want to paste the data, then do a paste.

Download multiple files when form submitted

I currently submit a form on to an invisible iframe. The action file creates a CSV which is automatically downloaded. Is it possible to have multiple files downloaded when the form is submitted?
I don't think that it is possible to do without having a custom download manager on the client side. You could technically insert them into the response stream to the client, but they wouldn't understand how to handle it.
I have always just created a zip file with all the files in it and sent that to the client. Wouldn't work if you are trying to display them in the browser or something like that, but I don't know what your goal is on the client so I thought that it might work for you.
Yes, the following code does what you want, with some modification. Note, very much not my code and it requires javascript be enabled.
function makeFrame( url )
{
ifrm = document.createElement( "IFRAME" );
ifrm.setAttribute( "style", "display:none;" ) ;
ifrm.setAttribute( "src", url ) ;
ifrm.style.width = 0+"px";
ifrm.style.height = 0+"px";
document.body.appendChild( ifrm ) ;
}
function downloadFiles( )
{
makeFrame('urlof/file1.csv');
makeFrame('urlof/file2.csv);
}
No, that is not possible. Each response can only contain one file. You need one request for each file that you want to download.