How to open, modify and save a file with javascript and ActiveX? - javascript

I tried different methods to open and show a file on client without to send the file to a server.
Opening: I tried to access the file with javascript from a <input type=file>, but the security restriction seems to not allow that.
I tried also <iframe src=file://local.path> with the same problem.
Last i tried with new ActiveXObject('Scripting.FileSystemObject');, but I get always an error that automationserver could not create the Object.
For saving I tried to use execCommand('SaveAs',true) from TextRange and DocumentFragment. From DocumentFragment I'm able to save a file but it contains only <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">. From TextRange is no save dialog shown.
What could I do?

From a web site, you cannot do any of this unless you use a signed script
From local file system, IE can use the filesystem object from an HTA

Sorry,
You can not access the local file system through a browser without having the user install a plug-in/activex object.
It may be that you should try another cross-platform delivery method, such as Adobe Air.

Related

Javascript to read Text File to Array without a Webserver or external libraries local on Windows PC

I have an HTML file with JavaScript that I am running without any Webserver/host so I am just opening the file in a browser local to my windows PC. In that HTML file I would like to be able to read a text file in the same folder as the html file. That file will contain data in rows and columns separated with tabs. i.e
1 a
2 b
3 c
I want to keep this as simple as possible so all I have to do is share the HTML and Text file to others so the can open it up local to their computer without any webserver/host and without having to also copy of external libraries like node.js or jquery.
I have searched and tested everything I can find but either I need to reference an external library or I have to run it in a webserver or I need to click a button to load the file through the browser, none of what I want.
Does native JavaScript support the function to read a text file and save it to an array? If so, any code direction would be great.
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
XMLHttpRequest() exists in native JavaScript, I think it will help you.
You also can send a request to the file. Or use library: axios.js because when you use XMLHttpRequest() you lose many time to write code which just get content from file, with axios I got file content with one line: `axios.get('file.txt').then(result => console.log(result.data));
To connect Axios: <script src="https://unpkg.com/axios#0.18.0/dist/axios.min.js"></script>
You can read official documentation about axios.js and XMLHttpRequest() in the net.

how to run excel application on html button through browser

i want to make a button in html page so that when a user click it an excel application get starts.
<button class="btn btn-primary" href="to popup excel sheet">click</button>
HTTP is a stateless protocol. What that means for you is that when your users download a file from the intranet via http, they are downloading a copy, rather than the original. Any changes they make will only appear in their copy, and then you end up with loads of copies of the same workbook with different, possibly overlapping changes. You don't want that!
And also ... how are your users even going to upload their changes?
You need to create a shared folder on your network and put the workbook there. You can then use the file:///SERVER/PATH/FILE.xls format in your <a /> links on your intranet to direct your user to the actual file on the server.
I would recommend you start by creating a simple html doc on your desktop to get familiar with the file:/// path format.
Eg
<html>
<head />
<body>
Click
<body>
<html>
save that in notepad and rename the extension from .txt to .html.
You can also type file:/// paths straight into windows explorer's address bar which allow for testing paths without resorting to the html document mentioned above.
UNFORTUNATELY! It seems that the browsers default behavior is to always download a link rather than open it (even if it is a local resource), so if you actually want to open it then you must resort to changing your browser intranet permissions to allow JS to access local resources, which then allows you to use the technique below.
This article (http://www.codeproject.com/Articles/113678/How-to-execute-a-Local-File-using-HTML-Application) uses
<script type="text/javascript" language="javascript">
function RunFile() {
WshShell = new ActiveXObject("WScript.Shell");
WshShell.Run("c:/windows/system32/notepad.exe", 1, false);
}
</script>
to open notepad. You can use command line arguments with Excel.exe (https://support.office.com/en-za/article/Command-line-switches-for-Excel-321cf55a-ace4-40b3-9082-53bd4bc10725) to tell it what the file path is...
Excel.exe "C:\PATH\Excel.xls"
Refer link: Open excel file through normal html link

Windows 8 Metro open Local files (.html)

I want to open a local html file from windows 8 metro (javascript ) App.
I tried doing it the way : http://msdn.microsoft.com/en-us/library/windows/apps/hh701484.aspx . It works fine as soon as i keep giving the actual http address but as soon as i replace them with my local file path , the success return is false everytime.
Any help ??
You can use the StorageAPIs and read all the HTML in a file. Then create a DOM element and set its innerHTML. (This is much easier if you use jQuery to manipulate the DOM).
I've got an example of something similar - where I read files from the app's local storage directory, and show the HTML in a web browser control. The example is in C# / XAML, but a similar logic can be used (without the need for a web browser control - since your app would be running inside a host that can directly show HTML like a browser):
http://krishnanadiminti.blogspot.com.au/2012/09/howto-provide-in-app-help-using-html.html

JavaScript API for Adobe Acrobat - how to create a relative link to a non-PDF document with JavaScript?

My company wants to set up some PDF documents to track projects. We want to create links inside the PDF that, when clicked, open a given document (.doc, .xls etc) using a relative path.
There is the app.openDoc JavaScript method, however that only works for PDF files.
There is the "Open a file" action, but that seems to work for only absolute paths.
I've looked into the API ( http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/js_api_reference.pdf ) to no avail ... does anyone know how to do this?
The doc object has no such method, but you can do the following:
Convert the .doc or .xsl file to .docx or .xslx
Attach the XML file to the PDF
Use the Doc.getDataObjectContents() to read the data
As an alternative, you can also use the util.readFileIntoStream method in a closed environment where you can install folder-level scripts on everyone's system.

how do I generate an xml file and allow the user to save it locally using javascript?

I am generating a XMl document via JavaScript and need to prompt the user to save it to their local machine.
This is the code I have at the moment:
var xmlWindow = window.open("", "");
xmlWindow.document.write("<node>data</node>");
This writes the xml to a new window which the user can then hit file->save to download.
Is there any more elegent way of doing this?Is there any way to set the mimeType of the new window so the xml displays properly?is there any way to specify the title so when the user hits save, the file name is set correctly?
note: needs to be compatible with most brosers (IE6, IE7, FF, Chrome).
You could send the XML through an XMLHttpRequest to a script on your server to make the xml file, then set window.location to the location of your new xml file. They'll be prompted to save it.
"save it locally": A tough call, if you want to be compatible across browsers. See this post.
TiddlyWiki manages to do this for some browsers (not sure which ones it supports) but only by having the user install certain files.

Categories

Resources