Access files from local machine - javascript

I need to access list of files in "D//logs" folder from my machine using javascript.
And then display them as hyperlink in a html page. I tried to acheive this using ActiveX, but accessing files is throwing error.
(Developing only for Internet explorer)
var fso = new ActiveXObject("Scripting.FileSystemObject");
var xx = fso.GetFolder("d:\\logs");
var yy = new Enumerator(xx.Files);
var zz = new Enumerator(yy.item()); // how to acess files inside folder

Related

How to AddFile using ActiveX in Plupload?

My machine is connected with a scanner. The scanner can return a couple of images and I can get the paths of these images in the web page. Then I want to upload these images using Plupload plugin. Now I can get these file using ActiveX. But when I call the AddFile function, it doesn't work. It seems like the file returned by ActiveX is not suitable for the AddFile function. How can I do ?
//scanFilePath such as: D:\image001.jpg
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f1 = fso.GetFile(scanFilePath);
var i_upload = $("#uploader").pluploadQueue();
var tmp = scanFilePath.split("\\");
var filename = tmp[tmp.length-1];
i_upload.addFile(f1, filename);

Javascript/JQuery Local File System

I have a problem regarding creating an XML file via a web browser and being able to read/write from it.
Multiple browsers have issues with this and I cant seem to find a solution that works.
I am creating a CMS which displays XML data in fields. I then want to read the values of those input elements and then overwrite the XML file using Javascript or JQuery.
The XML file will be stored on a web server and the client PC's will access the CMS via a webpage on the server and then reading/writing to the XML.
The Code below gives you an idea of what IM trying to do:
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
var fso = new ActiveXObject("Scripting.FileSystemObject");
var FILENAME ="xml.xml"; //<---- In Current Directory on Web server.
var file = fso.CreateTextFile(FILENAME, true);
file.WriteLine('<?xml version="1.0" encoding="utf-8"?>\n');
file.WriteLine('<Seating_Plan>\n');
var table = document.getElementById("table_assoc");
var rCount = table.rows.length;
for (var i = 1; i < rCount; i++) {
var id = table.rows[i].cells[0].children[0].value;
var deptcode = table.rows[i].cells[1].children[0].value;
var name = table.rows[i].cells[2].children[0].value;
var role = table.rows[i].cells[3].children[0].value;
var desc = table.rows[i].cells[4].children[0].value;
var image = table.rows[i].cells[5].children[0].value;
var asdir = table.rows[i].cells[6].children[0].value;
file.WriteLine('<Person id="' + id + '"><Dept>' + deptcode + '</Dept><Name>' + name + '</Name><Description>' + desc + '</Description><Role>' + role + '</Role><Image><image href="' + image + '"/></Image><AssociateDir><AssociateDir href="' + asdir + '"/></AssociateDir></Person>');
}
file.WriteLine('</Seating_Plan>');
file.Close();
}
However, This works only when accessing the webpage on a local system rather than the Server eg. via: "file://C:..." rather than:
http://localhost/admin.html
What is the best way to do this?
Any help would be appreciated.
Many thanks!
Aj
I suspect you're misunderstanding something here:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var FILENAME ="xml.xml"; //<---- In Current Directory on Web server.
The web browser can't access the server's file system. That ActiveX object may allow file system access on the current host, but it's not going to allow it on other hosts. If there are no errors and the browser is allowing the ActiveX object to interact with the local file system then I'll bet it's successfully creating the file on the local file system. (In whatever happens to be the current working directory for the browser process, since no directory was specified.)
To interact with files on the server's file system you'll need to use server-side code.

How to upload image file into the SharePoint 2010 picture library using ECMA Script

I need to upload image file into the sharepoint 2010 picture library using java script...
requirement is --
1.we have File Upload control
2.And, we have to upload image file from that file upload control
Please see code...But this code is not working (showing "Undefined object" exception for 'File' or 'FileInfo')
If any body have better solution that would be nice.
Thanks in advance.
<script>
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('Test');
//var fStream = (new FileInfo(uploadimagepath)).OpenRead();
var fStream = File.OpenRead(uploadimagepath);
//var fStream = FileUpload.PostedFile.InputStream;
//var contents = new byte[fStream.Length];
var newPic = oList.RootFolder.Files.Add(phototitle, fStream);
var oItem = newPic.Item;
oItem.set_item('Title', phototitle);
oItem.update();
oList.Rootfolder.Update();
clientContext.load(oItem);
</script>
you cannot use the uploadimagepath, it gives you the path from the client.
instead use the file stream which is sent from client to server.
check this url for understanding the file upload control better
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.postedfile.aspx

write bytestream to a activex object in javascript

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.

Find %LocalAppData% path and add new folder with JavaScript

I created a Windows 7 Gadget and I need to create a place on each user's computer to store the Settings.ini file (Created from my SettingsManager.js file). The application packaging team at my company recommends I use
%LOCALAPPDATA%\Microsoft\Windows Sidebar\
and then add
Gadgets\iMon.Gadget\
subfolders. This is so each user's settings are stored in a unique location and won't be changed by any other applications or gadgets.
Do I need to use something along the lines of
var fso = new ActiveXObject("Scripting.FileSystemObject");
var folder = fso.CreateFolder("path");
Any help on how to do this would be appreciated.
Update: I found how to get the %localappdata% path, but I still need to create the new folder. Here's what I've tried without success:
var wshshell = new ActiveXObject("wscript.shell");
var localAppData = wshshell.ExpandEnvironmentStrings("%localappdata%");
var filePath = localAppData + "\\Microsoft\\Windows Sidebar\\Gadgets\\iMon.Gadget";
var fso = new ActiveXObject("Scripting.FileSystemObject");
var filePath = localAppData + "\\Microsoft\\Windows Sidebar\\Gadgets\\iMon.Gadget";
I'm not sure I understand what you're trying to do here. is iMon.Gadget the name of your gadget? If so, this folder is automatically created for you upon installation of the gadget when the .gadget archive is executed. All the gadget's files and folders will be unpacked to
%LOCALAPPDATA\Microsoft\Windows Sidebar\Gadgets\iMon.gadget\
In your code, you can then proceed to manipulate files within this folder (and it's subfolders). To get the full path, you use
var gadgetPath = System.Gadget.path;
For example:
var oFile,
gadgetPath = System.Gadget.path,
oFSO = ActiveXObject("Scripting.FileSystemObject");
oFile = oFSO.CreateTextFile(gadgetPath + "\\test.txt", true);
oFile.WriteLine("Test.");
oFile.Close();

Categories