File Uploads and C:\fakepath\
I was recently working on a project that required providing an AJAX uploading widget to users. I decided to use Dojo's dojox.form.FileInput widget so the "Upload" button would look just like every other button within the web application. Everything worked great until I tested the widget in Chrome and found that the value of the input node was being set to C:\fakepath
\{Original File Name}. I then checked Internet Explorer and Safari; both of them were prepending "C:\fakepath
" to the file name. WTF?!
After doing some research, I found this blog post, which explained:
According to the specifications of HTML5, a file upload control should not reveal the real local path to the file you have selected, if you manipulate its value string with JavaScript. Instead, the string that is returned by the script, which handles the file information is
C:\fakepath
.This requirement is already implemented in Internet Explorer 8 - the real path to the file will be shown only if the page that contains the control is added to the trusted sites collection of the browser.
That made sense; essentially the browser is feeding that lame C:\fakepath
\ text in. Luckily all I needed to do was fix the issue by doing a simple string replace call:
// Change the node's value by removing the fake path inputNode.value = fileInput.value.replace("C:\\fakepath\\", "");
Whew -- dodged a bullet there. Just wanted to post this for everyone in case you run into it in the future.
I was always wondering whether the file-upload field is a security problem in general. couldn’t a you theoretically get any file from the client’s computer by manipulating the form-field’s value? But I’ve been too lazy to verify this…
Came across this exact same thing about a day before this post! Confused me at first too :)
this returns the real filename without the
'C:\fakepath\'
.this also works for:
this returns the filesize in bytes ;)
Martinjn,
Could you help me? With your code the
C\fakepath
don’t show anymore. But the file update is name on BD but won’t upload.Hi there, could you help me with how to implement this on a form what has the input file?
I get confuse. Thanks.
How do I use the
.files[0].fileSize
in the code below?does not work but
Works!
There is no need to use this
to replace the fakepath with empty string
as it may not work on all machines! as the path may be machine specific.
Instead use this
document.getElementById("fileInput").files[0].name;
to get the file name with out any extra string attached in the beginning. it will return only the file name.In my case i used
$("#uploadFile")[0].files[0].name
and it worked on html5 enabled safari browser as well!thank dude!
document.getElementById("fileInput").files[0].name;
works great!!!yes this work for 1 image, for example :
var b
not work !!!I’ve tried using javascript:
inputNode.value = newValue;
jQuery (change event on file input element):
$(this).attr("value", newValue);
In the latest firefox and chrome, both throw a security error and refuse to display the new value (ie, replace the fakepath crap).
Is there a way to change the display to get rid of fakepath that actually works?
This is good solution.
http://www.w3.org/TR/html5/number-state.html#file-upload-state
Thanks David. I also confused with it.
Hello,
I use the object
FileReader
on theinput
onchange
event the yourinput
file
type !Nice!That is the correct solution!
Don’t really care about what it shows, main thing is functionality
Hi,
I was trying to follow up an exercise in the book “Developing Backbone.js Applications”.
Your post helped me out with image path turned into C:\\fakepath\\.
This is what I did.
Good thing I found this blog! I was freaking worried about that.. lol
in Version 31.0.1650.57 this
document.getElementById(“fileInput”).files[0].fileName;
changes to
document.getElementById(“fileInput”).files[0].name;
Thanks you very much, that solved my problem…. :)
the results of my javascript on page :
i dont know if theres something repeated but it worked.
if there are multiupload, it’s works well
Hi
May be I make something wrong but this way remove all text from input.
So I’ve solved “fakepath problem” by
document.getElementById("uploadFile").value = this.value.substring(12);
The best answer is the simplest one.. Thank You Savin!
Hi Jérémy D’Hasseler
you answer has solved my problem,Thanks!
Please, how do I fix this, where do I enter this code to fix the fakepath issue.
Thanks.
document.getElementById("fileInput").files[0].name;
worksHi Savin !!! your code is working but its not working in mozilla firefox any other solution for firefox ?
Hey !!! Any on can tell me how can this code working, I am using it so many times… Please Help :::
How it works in the Chrome?
I use the
document.getElementById("fileInput").files[0].name;
int the Chrome, I just get the file name, but i want to get the full name likeD:/xx/xx/xx.doc
We get the file name but how can we move this file to a different directory since it only has filename and not the actual path. I am trying to send email with attachment but I get path errors due to this. Any solutions?