Get file path through javascript - javascript

I am having a file upload control in my page.
I want to get the full path of the uploaded file by javascript.
i have written
document.getElmentById('fileControl").value;
It is showing the full path in IE but in mozilla 3.5 it is showing only the filename not the path.
So how to solve this problem ?

This will be a security issue. Reading the file path of the client machine is not a good idea and won't be allowed by most of the browsers.

Firefox doesn't let you see the path to the file by default - apparently there is a workaround for it but it requires the user to go to about:config and change a setting, which let's face it, ain't going to happen most of the time.
If you absolutely need this (I can't see why though) then you could look at using Flash as a cross-browser alternative, google for Flash and file uploads.
Otherwise, you'll need to work with the limitation here (which in my mind is a very good one!)

Related

Javascript: how I can read files and folders from usb flash drive

I want print PDF files from usb flash.
Now I decide do this with Mozilla Firefox and Plugin R-kiosk trought open libruary PDF.js
But still have a problem: how read folders and files to build tree without <_input /> cause it allow user get in system.
Any idea? Thx
May be you can give advice the best way to realise idea without browser... But I don't work with pdf libruaries, output and rendering of pages I have simply understanding, but I can't say this about silent printing of document
I don't believe this is possible. Browsers like Firefox generally won't let you have access to the local file system / disk structure for security reasons. Even if you knew the local path of the PDF, it wouldn't be of much help.

Creating an offline version of pdf.js by combiing pdf.js and pdf.worker.js

I am attempting to create a javascript pdf reader that can work offline in the browser (it uses the browser but not the internet), and am using Mozilla's pdf.js to do it. Pdf.js has a main file of the same name, which uses the internet to inject script from a pdf.worker.js file. One obstacle to creating an offline version of pdf.js is to stop it from using the internet to do this, by either pasting the pdf.worker.js code into the parts of pdf.js code that ask for it, or to convert the pdf.worker.js into a Data URI and use that as the url that pdf.js asks for. Doing the former has not yielded any success, and doing the latter has only worked on Firefox. We want this to work any way we can make it work, so I have pretty much two questions pertaining to making pdf.js offline.
1) If I am going with the method of physically pasting pdf.worker.js code into the spots where pdf.js uses the internet to inject pdf.worker code in, how would I make it work? I tried doing it, and I couldn't get it to work. Are there any things I must pay attention to?
2) I have had more success with converting pdf.worker.js into a Data URI and having pdf.js use that instead of the url it was using to access worker.js. However, this process only works on Firefox. Any idea why? And, do you know of a way I can make this process work with the other browsers?
Any input is welcome. Thanks in advance!
Go to https://github.com/mozilla/pdf.js/
Follow the instructions in "Getting the Code" and "Building PDF.js"
Then run node make singlefile
Combined file will be in build\singlefile\build\pdf.combined.js
Worked for me on Windows. Should work on other platforms too.

getJSON without showing the file path

I was wondering if there was a way I can get a json file (currently using $.getJSON) without showing the get url in the 'view source'
Not really. I mean, you could derive it from something else via a complex function, but even then, it's there, just obscured.
And if you did, someone could just as easily snoop the file path using the browser's built-in diagnostic/debugging tools as they could from View Source (the "Network" tab in Chrome, for instance — all major modern browsers have debugging tools built in now). Here's me snooping on the path Stack Overflow uses to give details of upvotes/downvotes (for those with enough rep to see the breakdown):
Or they could use the debugger (see the "Scripts" tab) to inspect the string variable the calculation ended up with. Etc. Basically, if the browser knows enough to be able to retrieve the resource, the user can find out what that path was.
The only thing I can think of is to use a plug-in, like Flash or Java, to retrieve the resource and then display it. That would raise the bar a little (the path would still be accessible to anyone with a network analyzer or proxy).
Nope. Security through obscurity is never a chance.
You could make harder to reuse it by adding some referer check on the server side, though..
Or by using some "signature" token, etc.. (if you want to prevent people using it as a webservice).

How to save an image with JavaScript?

I'm working on a Chrome Extension: when you drag an image, it will be saved to your computer.
I learned that in HTML5 there is FileWriter API, but really can't find any example of it, and does Chrome support it?
No, there isn't a FileWriter API in HTML5. What you probably mean is the File API that allows you to read files. And in Chrome even extensions aren't allowed to write files, for reasons of security. So unless you want to bundle an NPAPI plugin with your extension (which would trigger a huge warning upon installation) all you can do is trigger a download message that the user might choose to accept - or not. See Cross-browser Save As .txt for a possible approach (Flash objects like Downloadify being the other).
Edit: I was wrong, there is a FileWriter API proposal. It is very far from being done however.
I found this. http://www.html5rocks.com/en/tutorials/file/filesystem/
You can find some examples.
Edit This is screenshot from the article. I'm using chrome 12.0
FileWriter API browser support message http://s3.amazonaws.com/twitpic/photos/full/329503613.png?AWSAccessKeyId=AKIAJF3XCCKACR3QDMOA&Expires=1308932374&Signature=DXBdFSjbNqaeJPr%2F0fSAqPWyh2E%3D
I don't think the FileWriter API will be ready and usable for some time yet.
You could get the image data in hex, then use a DataURI to 'export' it from the browser. Although this leads to a file saved with a filename such as "download(1)". Each browser seems to have different size limitations for DataURIs, and they're not big, although ahould be fine for a reasonably sized image.
http://en.wikipedia.org/wiki/Data_URI_scheme
Alternatively you could use a Downloadify to save it with a proper filename (Requires flash and may be tricky to embed into the chrome extension).
http://davidwalsh.name/downloadify

javascript jquery file opener

i was wondering if anyone knew a quick solution to my problem. I want to be able to open a folder on users screen pretty much the same as but just opening up the my computer folder so a user can drag a file onto the browser screen. anyone know how?
You can't. They even gave this bug a name: "security".
The closest you can get without using Flash, proprietary browser stuff or Java is something like this.
You have to use a java applet.
Javascript has no access to the file system for security reason.
Every page would be able to see the files on your local machine and might display: "According to the files on your disk you might be interested in some of our special movies ;)"
Incidentally, there are custom extensions in IE and Firefox that let you load and save files; it's the reason TiddlyWiki can work as it does. It does degrade a Java applet for other browsers.
A new jQuery plugin has been extracted out of TiddlyWiki to give Javascript authors the ability to load and save, and it's portable across all browsers:
twFile - http://jquery.tiddlywiki.org/twFile.html
It's quite possibly not what you're after as it will only work off a file:/// URL.

Categories