I'm trying to read a xlsx file in html using javaScript but it seems my code is not so right as it should be.
I'm using this code:
<script>
function openRead(){
var excel = activeXObject("Excel.Application");
excel.visible = false;
var file = exel.workbooks.open("C:\Users\Utilizador\Desktop\teste.xlsx").activeSheet.cell(1,1).value;
var div = document.getElementById("div1").text;
div = file;
}
</script>
Getting the following error
activeXObject is not defined
I hope you guys can point me to the right direction!
ActiveXObject is available only on IE browser. So every other useragent will throw an error
https://stackoverflow.com/a/11101655/1172872
You can try existing libraries... (or see how they implemented it)
http://sheetjs.com/ (https://github.com/SheetJS/js-xlsx)
http://oss.sheetjs.com/js-xls/
Related
I have some javascript code that allows users to drag and drop files in IE.
The problem I have is that they would like the file deleted after being dragged, but I can't seem to get the file path, just the name.
I have seen many references to e.dataTransfer.files[0].path, but it always comes back as 'undefined' when I try it.
Any ideas why e.dataTransfer.files[0].path does not work, or how I can get the file path?
$(document).ready(function (ex) {
var holder = document.getElementById('holder');
holder.ondragover = function () { this.className = 'hover'; return false; };
holder.ondrop = function (e) {
e.preventDefault();
var file = e.dataTransfer.files[0];
var path = e.dataTransfer.files[0].path;
fileArray.push(file);
//alert(e.target.id);
var reader = new FileReader();
reader.readAsDataURL(file);
};
});
that's not possible for a web app, but if your users would run an app on their machine, you could build an electron app. inside electron you get the e.dataTransfer.files[0].path property, which you can then use to delete the file.
https://www.electronjs.org/
The file doesn't have property path.
You can experiment with it here and the complete list of file properties can be found here.
To read the file one uses FileReader as you did, without using the path explicitly.
After trying everything I could find, I am of the opinion that it is not possible.
The below code works fine on google chrome only. Other browsers return Only mp3 file type allowed. Help fix please
$("#song").change(function () {
var file = this.files[0];
var songfile = file.type;
var match = ["audio/mp3"];
var addResult = "#music_formresult";
$(addResult).empty(); //remove previous error message
if (!((songfile == match))) {
$(addResult).html("**Only mp3 file type allowed.**");
$("#songName").html("Select Song");
return false;
} else {
var filename = $('#song').val().split('\\').pop();
$("#songName").html(filename);
}
});
You are comparing a string to an array, which is weird but probably will work fine on all browsers.
You have extra parenthesis on the if statement but that isn't the cause either, however I would remove them anyways.
My guess is the following is not being consistent across all browsers
var file = this.files[0];
var songfile = file.type;
In order for me to be able to help you, please provide a codepen sample of your code running, or place a breakpoint inside the function and show me the value for the above lines on a browser that is not Chrome.
I got a a script wherein I want to convert the extension to another one then open it using a specific application.
For instance, I got .mht file located in my Desktop. An html file with internal javascript on it.
What I want to happen is when I open the HTML file on internet explorer and click on the hyperlink, it should convert .mht file into .docx and open it using Microsoft Word. Unfortunately, my below code does not work, if i click the hyperlink, it does opens up Microsoft Word but giving me an error message saying that the file cannot be found. Can someone assist me with this please? Thanks in advance, will much appreciated.
<HTML>
<HEAD>
<script type="text/javascript">
<!--
function openDocument(file)
{
try
{
var Word = new ActiveXObject("Word.Application")
var file;
file = file.split(".");
file = file[0]+".docx";
Word.Visible = true
Word.Documents.Open(file)
}
catch(exception)
{
if(Word)
{
alert(exception.message)
Word.Quit()
}
window.location.href = file
}
}
// -->
</script>
<TITLE>Launch Word - Local</Title>
</HEAD>
<BODY>
Summary
</BODY>
</HTML>
For security purpose, Javascript can't write on the filesystem, so it is impossible to change the extension of a file.
You should be able to do something with the new FileSystem API (check that tutorial : http://www.html5rocks.com/en/tutorials/file/filesystem/), but it is available only on Chrome (http://caniuse.com/#feat=filesystem)
EDIT:
With an ActiveX, using GetFile may work:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var file = fso.GetFile("c:\\myfile.mht");
file.name = "newName.newExt";
how can i append data to a file using javascript?
i tried to use this code, but i got an error:
var fso = new ActiveXObject("Scripting.FileSystemOject");
var filepath = fso.GetFile("member.txt");
var fileObject = fso.OpenTextFile(filepath, 8);
file.WriteLine(id + "|" + pass);
fileObject.close();
the error is on var fso = new ActiveXObject("Scripting.FileSystemOject");, written: Error: Automation server can't create object
is there any other way to append the file using javascript or the way to fix this? thanks :)
EDIT:
i have doing what's written on this, and it still not working :/
I just realized these in your code:
var fileObject = fso.OpenTextFile(filepath, 8,true);
You'll need the true-argument, if the file does not exist, or you want to overwrite/append it.
var filepath = fso.GetFile("member.txt");// This won't work.
var filepath = "your_filePath"; // Use this instead
var fileObject = fso.OpenTextFile(filepath, 8, true);
OpenTextFile() needs a path as a string like "D:/test/file.txt". GetFile() returns an object, which you can see as a string (D:\test\file.txt), but it's not a string. Use also absolute paths, relative paths don't seem to work by my experience.
EDIT
Add the code below to the <head>-part of your html-file, then save locally as a hta (with file extension hta, not htm or html).
<hta:application
applicationName="MyApp"
id="myapp"
singleInstance="yes"
/>
Then run the hta-file. If you still getting an ActiveX-error, it's not supported by your OS. If this works, you haven't done all the security settings correct.
EDIT II
In this case it's not very usefull to get the path through ActiveX, you'll need to write it literal anyway. And I'm not supposed to do your homeworks, but this does the trick...
var filepath = new String(fso.GetFile("member.txt")).replace(/\\/g,'/');
And don't forget what I've said above about using absolute paths...
The 8 in the OpenTextFile function specify that you want to append to the file. Your problem comes from the security restriction of your browser. To make it work you'll have to lower the security level, which is not really recommended.
The error is thrown because there are security restrictions which donot allow the activex to run. change your security settings to allow the activex if your using internet explorer (which i think you are).
This might be useful http://windows.microsoft.com/en-US/windows/help/genuine/ie-activex
Cheers
EDIT: i have doing what's written on this, and it still not working :/
* try Restarting your browser
As pointed out in this comment
Javascript: how to append data to a file
the cause of the error Error: Automation server can't create object is the typo in the progid passed to ActiveXObject: Oject instead of Object:
var fso = new ActiveXObject("Scripting.FileSystemOject");
there is a missing b!
I am trying to create a file on the local machine which captures the var file in javascript.
<script>
function button_click()
{
var file = GetFile('Getdoc'.aspx');
WriteToFile();
}
function WriteToFile() {
var fso, s;
fso = new ActiveXObject("Scripting.FileSystemObject");
s = fso.CreateTextFile("C:\\Test\\Logfile.txt");
s.Write(file1);
s.Close();
}
</script>
Here we get a httpresponse stream which contains data in bytes into var file.
If I could find some help on this would be appreciated.
Thank you.
If I understand you correctly, you are encountering a problem when you are trying to write a file using ActiveXObject on IE? There is two things wrong with your script.
var file = GetFile('Getdoc'.aspx'); should be var file = GetFile('Getdoc.aspx');
You do not have file1 defined, so it is writing nothing to the file.
You need to keep in mind that most versions of IE have this functionality disabled due to the huge security risk that this poses.