jquery string replace
need help on this, trying to upload images from IE9 I get an C:fa开发者_StackOverflow中文版kepath/name_of_my_file
how do I remove this C:fakepath?
Thanks
if($('#ficheiro').val().search(/C:fakepath/)) {
$('#ficheiro').val().val($('#ficheiro').val().replace('C:fakepath',''));
nome.val('pics/'+$('#ficheiro').val());
} else {
nome.val('pics/'+$('#ficheiro').val());
}
You can use this and it will also remove the slashes leaving you just the filename.
$(this).val().replace(/C:\\fakepath\\/i, '');
Can't you simply use the replace function of string?
nome.val("pics/" + $('#ficheiro').val().replace("C:fakepath", ""));
var path = new String($('#ficheiro').val());
path = path.replace("C:fakepath", "");
Have a solution for you, firts check for browser beeen IE, next use encodeURI to encode all the file path and name, you have to do that first in order to capture correctly the unscaped chars like "\". Then just replace, its working for me:
var browserName=navigator.appName;
if (browserName=="Microsoft Internet Explorer")
{
var soloNombre = encodeURI(soloNombre);
soloNombre = soloNombre.replace("C:%5Cfakepath%5C","");
var soloNombre = decodeURI(soloNombre);
alert(soloNombre);
}
Works like a charm.
Try this
$(function() {
$("input:file").change(function (){
var fileName = $(this).val().replace("C:\\fakepath\\", "");
});
});
精彩评论