1

I try to get the upload value when the input field's change event occurs

But I get this value on Chrome,

C:\fakepath\winnie-the-pooh.jpg

So I split the \ and the the last item in the array.

jquery,

$("#upload").change(function(){

    var fragment = $("#upload").val();
    var array_fragment = fragment.split('\/');
    alert($(array_fragment).last()[0]);

});

But I still get C:\fakepath\winnie-the-pooh.jpg instead of winnie-the-pooh.jpg on Chrome.

How can I make it worked?

the html,

<input type="file" name="image" id="upload"/>
Run
  • 54,938
  • 169
  • 450
  • 748
  • there is no way to get the fullpath in chrome. http://stackoverflow.com/questions/4851595/how-to-resolve-the-c-fakepath – Zernel Dec 30 '13 at 09:22

3 Answers3

3

You need to split on \, not /:

var array_fragment = fragment.split('\\');

Ideally though, you'd split on either. Fortunately, split takes a regular expression:

var array_fragment = fragment.split(/\\|\//);

That regex basically means "\ or /"—the pipe operator, |, is the "or", and the / characters at the start and end signify that a regular expression is in between.

Samir Talwar
  • 14,220
  • 3
  • 41
  • 65
2

You do not need jQuery to operate with arrays. Also, the file path may not contain a slash, e.g. on Firefox. Consider this example:

var filename = $("#upload").val().replace(/.+[\\\/]/, "");
alert(filename);

This is better than wrapping an array with jQuery just to get its last element.

sanmai
  • 29,083
  • 12
  • 64
  • 76
  • If there's no slash, then `split` will return an array of one element (the entire input), so the last element will still be the file name. That said, I do like this answer. Shorter is generally better. – Samir Talwar Sep 04 '11 at 16:09
0

you can read my post i had asked months ago:: my question

based upon your requirements you can derive something from that post!!!

Community
  • 1
  • 1
abhijit
  • 1,958
  • 3
  • 28
  • 39