views:

34

answers:

2

I am wondering where the file path is stored in a File object in HTML javascript.

I used the Webkit DevTools and got this:

FileList
0: File
    fileName: "script.js"
    fileSize: 71268
    name: "script.js"
    size: 71268
    type: "application/x-javascript"
    __proto__: File
length: 1
__proto__: FileList

The file name, size and types are there(anyone knows why name and size have 2 variables), but the path is not.

Is there any way to find path of the file, and if not, how does the browser and javascript read the file(such as POST methods & determining the type and size)?

+1  A: 

My guess is that it's not accessible for security reasons.

Matthew Crumley
+1  A: 

As you can read in the HTML 5 Working Draft,

[f]or historical reasons, the value IDL attribute prefixes the filename with the string "C:\fakepath\". Some legacy user agents actually included the full path (which was a security vulnerability).

Reading on on MDC, we can see that Mozilla's implementation of the File object has a (non-standard) property named mozFullPath, containing

[t]he full path of the referenced file; available only to code with UniversalFileRead privileges in chrome.

That page also answers your question about the redundant data in the File object: properties fileName and fileSize are deprecated. Also look at W3C's File API Working Draft, where those are not mentioned.

To answer the second part of your question:

if not, how does the browser and javascript read the file (such as POST methods & determining the type and size)?

Of course, internally the complete file path can be accessed (and is in several browsers shown to the user), but it's not accessible to JavaScript scripts running in a web page.

BTW, a few years ago there was a discussion about this on the WHATWG mailing list.

Marcel Korpel