开发者

jquery get the input file value on Chrome

开发者 https://www.devze.com 2023-04-02 20:30 出处:网络
I try to get the upload value when the input field\'s change event occurs But I get this value on Chrome,

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,

$("#uplo开发者_如何学运维ad").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"/>


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.


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.


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号