Johnrad
Johnrad

Reputation: 2655

Use javascript to get the directory of a file

I am using an input of type=file and I am trying to figure out how to extract the file location from it. I am using this code:

file = $("#uploadFiles").attr("files")[0];
var fileName = file.fileName;
var formData = 'uploadFile=' + fileName;

and when i alert formData, it says "uploadFile=temp.jpg"

What I want is the alerted message to be something like:

"uploadFile=C:\user\doug\documents\pictures\temp.jpg"

But I don't know the attribute of the file object to put in for file.fileName

Upvotes: 2

Views: 622

Answers (2)

alex
alex

Reputation: 490263

Generally, you can only rely on the filename being accessible - used mainly as a preliminary extension checking (e.g. (jpe?g|png|gif)$) on the client side (which only serves to benefit the user, to stop them from uploading a 5mb file that will be not validate on the server anyway).

You can access whatever the browser will give you with...

jQuery

$('file[type=input]').val();

JavaScript

document.getElementById('file-input').value;

Upvotes: 3

SLaks
SLaks

Reputation: 887453

For security reasons, this is completely impossible in modern browsers.

Upvotes: 3

Related Questions