How to move a file with JavaScript - javascript

I want to move a txt file from drive c to drive d and I found this code by searching but it does not work properly.
please guide me.
Thanks
<html>
<body>
<script language="JScript">
function move() {
var object = new ActiveXObject("Scripting.FileSystemObject");
var file = object.GetFile("C:\\1.txt");
file.Move("d:\\");
console.log("File is moved successfully");
}
</script>
<button onClick="move()">Move File txt</button>
</body>
</html>

Browsers do not provide any features that let code provided by a webpage move files the users' hard disks.
The code you've found may have worked in old versions of Internet Explorer (I think the feature was removed in later versions) but only when the security settings were altered from the default to allow it.
You could probably use it in server-side Classic ASP (but then it would move files on the server rather than the client).
For a browser-style UI which can do this, look to tools like Electron which pair a custom browser with Node.js in a desktop application. You can then use the Node.js side of your custom application to move files.
Obviously this will require that the user download and install your application and use that instead of their web browser.

Related

pdfjs cannot use function 'getPageIterator'

I am trying to follow the documentation for pdfjs found here https://www.pdftron.com/documentation/core/guides/features/manipulation/remove/ in an attempt to remove a page from a PDF I have uploaded to my html page. Here is my html code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.2.228/pdf.js"></script>
<input type="file" id="input"/>
<script>
document.getElementById('input').addEventListener('change', function(e){
var reader = new FileReader()
reader.onload = function(x){
window['pdfjs-dist/build/pdf'].getDocument({data:x.target.result}).promise.then(function(doc){
doc.pageRemove(doc.getPageIterator(5));
console.log(doc.numPages)
})}
reader.readAsBinaryString(e.target.files[0])
}, false)
</script>
which gives this console error when I upload a PDF file to the page:
removeDemo.html:10 Uncaught (in promise) TypeError: doc.getPageIterator is not a function
The PDF I am uploading has more than 5 pages, so asking to remove the 5th page in particular shouldn't be the problem. Other functionality does seem to work however, for example, I have a line in the above code that prints the number of pages of the document. This works fine when I comment out the 'getPageIterator' line. So it seems to be a problem with this specific function, rather than a more general problem. I would like to know what is causing this problem. In case this is relevant, I am running this in chromium on a macbook pro.
Please let me know if there is something in the above question that I can further clarify.
Mozilla led pdf.js is primarily a browser plugin pdf viewer, without editor functions.
The function your calling doc.pageRemove(doc is for use with PDFTron webview / edit SDK and thus specific to that commercial JavaScript library.
The documentation you linked to, from PDFTron, has nothing to do with PDF.js, it is a completely separate SDK. This is why you get an error making a SDK API call on a different SDK.
Since PDF.js does not support removing pages from a PDF (nor editing in general), then I assume your intention is to use PDFTron SDK to remove (or edit in other ways) a PDF file in the browser client side.
In which case you want to do the following.
See this sample:
https://www.pdftron.com/documentation/web/samples/pdf-manipulation/#page-operations
See here to get started with the SDK:
https://www.pdftron.com/documentation/web/get-started/

Access Language JSON file with JS on Chrome without a webserver

tl/dr: How can I
internationalize strings in a html5/javascript application
while using a json file or something similar with key/value pairs (easy to translate)
without using javascript vars for every language string (ugly)
and if possible, without complex frameworks or packages
on Chrome (or something with same-origin-policy)
without a (local) webserver
without internet connection
Details:
I am developing a html5 touch game for older useres on an embedded IE system that will be changed to an embedded chrome system soon. Using a webserver is currently no option and I can't assume I have an internet connection all the time. Since the application should be in different languages, I currently have a json file that is accessed like this (irrelevant stuff left out):
//...
var language = "en"; //the language we want, same as the json file name
var key = "key"; //the key to the value we like to obtain
var languageMap;
var langFile = $.getJSON(language + ".json", function(data){
languageMap = data;
});
var langFileStatus = $.when(langFile);
langFileStatus.done(function () {
var value = languageMap[key];
//use the value of "key" here for awsome stuff
});
//...
the language file (e.g. "en.json") looks like this:
{
"key":"value",
"otherKey":"otherValue",
}
which works pretty well for IE and FF, but not on Chrome, because of the same-origin-policy. I read about an awsome trick to bypass that here, but I couldn't make it work in this case. I have never used JSON before in connection with JS, so maybe its an easy question. Different solutions for the whole problem are also very welcome (thats why I posted the complete problem). Thanks in advance!
Download Web Server for Chrome App from here. This is not exactly a server, but a handy simulation of the same that allows you to run your files locally as if they are running on a server.
It works without any Internet connection. More importantly, it has configuration options for CORS request thanks to recent updates Install it, select the folder in which your files are present, and you are ready! It's a really good way to test your code locally on Chrome.

Deleting the file from client PC after upload but only after client permission

I got a requirement from client to delete file from his local pc after upload. It should ask to user if he want to delete the file after successful upload. If user opted yes then file should be deleted from client pc.
I know this is not easy to achieve in web application while quite easy in desktop app.My client is upgrading from desktop app to web and expecting the same behavior.
I heard that any browser plugin or small utility installed on client machine can do that. I have seen few example in other website that client is referring.
Can someone please suggest me the plugin or utility logic that can help me to achieve this? And how we can interact with these stuff from our java script or code.
Thanks in advance.
Regards,
Krishan
Javascript/HTML5 alone can't do this, it's restricted to maintain a certain level of security. You will have to look into activeX plugins, and it will only work if the user runs the web app on IE.
Here is a short example:
<script type="text/javascript">
// initialize ActiveXObject with Scripting.FileSystemObject:
var activeX_FileSystemObject = new ActiveXObject("Scripting.FileSystemObject");
if(confirm("Delete file?"))
{
activeX_FileSystemObject.DeleteFile("C:\\myFolder\\myFile.txt", true);
}
// Another way (multiple files in a catalog):
if(confirm("Delete file?"))
{
activeX_FileSystemObject.DeleteFile("C:\\myFolder\\*.txt", true);
}
activeX_FileSystemObject = null;
</script>
It's also possible that a Chrome plugin can do the same as the activeX, but never coded one myself - and it's also Chrome spesific.

Javascript Access to File on local machine

I want to open the files located on local drive using window.open().
When i try to access the file using window.open i am getting error "Access is denied."
Would somebody help to achieve this requirement in Internet explorer 8.0?
Thanks!
You can't. And thank God for that. Imagine how insecure the internet would've been if JS was able to access a client's file-system.
Of course, IE8 has the MS specific JScript superset (ActiveXObject), which does enable filesystem access:
var fileHandle,
fs = new ActiveXObject("Scripting.FileSystemObject");
fileHandle = fs.OpenTextFile("C:\\path\\to\\file.tmp", 1, true);
fileHandle.Write('This is written to a file');
console.log(fileHandle.ReadLine());//will log what we've just written to the file
But this is non-standard, is - I think- no longer supported either, and doesn't work X-browser.
Here's the documentation. At the bottom there's a link to a more detailed overview of the properties and methods this object has to offer, as you can see, there's a lot to choose from
I'm adding this answer just to be complete, but so far as Web Pages go, Elias Van Ootegem's answer is correct: you can't (and shouldn't be able to) get to the local hard drive.
But .. you can isf your page is an HTA (HTML Application) :
HTML Application wiki
This is essentially a web page with .hta as the extension(usually) and some extra tags to tell IE that it's an HTA application, not a web page.
This is something that runs via the windows operating system and is so far as I'm aware only available for IE. The HTA application opens as a web page in IE, but without the usual web navigation / favourites toolbars etc.
Note that if you have a page on an internet server delivered as an HTA application, you're likely to cause virus scanners and firewalls to pop up because this would essenstially be running a script whcih could do manything to your computer. Not good for general internert stuff at all, but might be useful in a secure environment like an intranet where the source of the application is known to be safe.
To get to the file system, you can use javascript code like this :
// set up a Fils System Object variable..
var FSO = new ActiveXObject("Scripting.FileSystemObject");
// function to read a file
function ReadFile(sFile) {
var f, ts;
var s="";
if(FSO.FileExists(sFile))
{
f = FSO.GetFile(sFile);
ts = f.OpenAsTextStream(ForReading, TristateUseDefault);
if (!ts.AtEndOfStream) {s = ts.ReadAll( )};
ts.Close( );
}
return s;
}
alert(ReadFile("c:\\somefilename.txt");

Why does Cordova 2.7.0 JS seemingly no longer work on remote pages?

Background
I'm attempting to upgrade an iOS app built on Cordova 2.0 to version 2.7.
It's basically a welcome screen that points to a remote search engine (please withhold comments about app validity and likely approval, as we're past that), and we were using the ChildBrowser plugin to enable opening links in a sub browser so as not to trap the user in the Cordova webview.
Cordova 2.7 has a feature called InAppBrowser I am hoping to use instead of ChildBrowser. InAppBrowser does essentially the same thing, aside from missing a button to open in Safari.
Problem
The existing app's remote webpages include the Cordova JS (as well as that for the ChildBrowser plugin) and it works fine for opening links in the sub browser.
My test Cordova 2.7 app doesn't seem to load the Cordova JS correctly when it's being loaded from a remote web page.
I tried using this exact same HTML on the embedded start page and a remote start page:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="http://mydomain.com/mobile/cordova-2.7.0.js"></script>
</head>
<body>
<script>
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
alert("Ready!!");
}
</script>
</body>
</html>
To test this as the embedded start page, I put this line in config.xml:
<content src="index.html" />
When I run the app, I promptly get the "Ready!" alert.
To test this as the remote start page (I'm aiming to link to the remote page in the final app, I am just using it as the start page for testing. The result is the same if I link from the embedded page.), I put this line in config.xml:
<content src="http://mydomain.com/mobile/index.php" />
When I run the app, I just get the blank screen and no alert.
Further, in cordova-2.7.0.js L. 6255, I changed
console.log('deviceready has not fired after 5 seconds.');
to
alert('deviceready has not fired after 5 seconds.');
With that change, running the app using the remote start page causes the blank page, and then after five seconds, I get the alert "deviceready has not fired after 5 seconds.". So this tells me Cordova JS is not starting correctly. Needless to say, I can't get InAppBrowser to launch links in the sub browser on the remote site, but I can get it working just fine on the embedded start page.
Anyone have any ideas of where to go from here? This is a pretty simplistic example, so I'm assuming this is a Cordova settings problem or a change in the functionality. I appreciate any thoughts, thanks!
Yes, something broke in 2.7 - related to our cordova-cli work. See: https://issues.apache.org/jira/browse/CB-3029
The fix is to add an empty file called "cordova_plugins.json" in your root folder.
I had a similar problem relating to upgrading to Cordova 2.7. However my problem was all my console.logs stopped firing when running the app. I couldn't figure out why for the life of me this was happening. I thought it was because I upgraded jquery.mobile. That wasn't it. I then thought it was an .htaccess issue, that wasn't it either. It turns out, it was Cordova 2.7 that was causing this problem.
I did try adding the .json file on my server, that did not fix the issue.
The fix was going into the 2.7 source and commenting out the following code:
/*comment out this as it is breaking console.logs
var xhr = new context.XMLHttpRequest();
xhr.onload = function() {
// If the response is a JSON string which composes an array, call handlePluginsObject.
// If the request fails, or the response is not a JSON array, just call finishPluginLoading.
var obj = this.responseText && JSON.parse(this.responseText);
if (obj && obj instanceof Array && obj.length > 0) {
handlePluginsObject(obj);
} else {
finishPluginLoading();
}
};
xhr.onerror = function() {
finishPluginLoading();
};
xhr.open('GET', 'cordova_plugins.json', true); // Async
xhr.send();
*/
Replace entire block with a call to the following function:
finishPluginLoading();
My logs are now working again. Only took me 3 days scratching my head.
Hope this helps someone with a similar problem.
If you embed Cordova in the external web page, there will be no way to open the InAppBrowser from within your hybrid app, so Cordova will not be able to load. This is because the InAppBrowser requires Cordova to be fully loaded and initialized before it can be used to fetch a remote page. You need to use your HTML page that you have, with the <script type="text/javascript" src="http://mydomain.com/mobile/cordova-2.7.0.js"></script> as the main entry point for your app. Then you can use the InAppBrowser to open up your remote page. (You could probably do this in the onDeviceReady(), not sure if it would "flash" the page first though.) I don't think the remote page should have any Cordova code in it at all. I'm not sure if it would be possible to even interact with Cordova from the remote page due to the Same Origin Policy (probably you could use features of the InAppBrowser to inject "bridge" code though to get around this.)
As Shazron mentioned the problem is the issue with the file"cordova_plugins.json".
To solve the problem not changing the code you can create the "cordova_plugins.json" file in the root folder and insert a content between quotation marks inside this file.
Mine for example has the following content:
"Just a dummy file required since Cordova 2.6.0"
create a file cordova_plugins.json that contains {}. then go to cordova-2.7.0.js and comment this line require('cordova/channel').onNativeReady.fire(); then when development done, add it back
Like me if you are using Cordova 5.1.1 and want to access native functionality after redirect then copy cordova.js, cordova_plugins.js and plugins folder which is at \platforms\platform_name\assets\www\ and put them on server, finally reference cordova.js inside your html. After every plugin add make sure to update these files and folder.

Categories