javascript 如何使用java脚本在文件上传中获取没有扩展名的文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20323999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get file name without extension in file upload using java script
提问by nksingh420
i am going to upload file using file upload control in asp.net . now i want to get file name without extension using java script . i want to validate file name should be only integer format
我将使用 asp.net 中的文件上传控件上传文件。现在我想使用 java 脚本获取没有扩展名的文件名。我想验证文件名应该只是整数格式
$(function () {
$('#<%=File1.ClientID %>').change(function () {
var uploadcontrol = document.getElementById('<%=File1.ClientID%>').value;
})
})
回答by Andrew
Try this:
试试这个:
$('#test').change(function() {
//something like C:\fakepath.html
var fpath = this.value;
fpath = fpath.replace(/\/g, '/');
var fname = fpath.substring(fpath.lastIndexOf('/')+1, fpath.lastIndexOf('.'));
console.log(fname);
});
回答by AurA
You can split it with respect to last instance of dot('.')
您可以根据 dot('.') 的最后一个实例拆分它
var uploadcontrol = document.getElementById('<%=File1.ClientID%>').value;
uploadcontrol = uploadcontrol.split('.')[uploadcontrol.split('.').length - 2];
But,
但,
this is valid for simple case only... a much better generic answer is given at How to get the file name from a full path using JavaScript?
这仅对简单情况有效......如何使用 JavaScript 从完整路径获取文件名给出了更好的通用答案 ?
Take care of these things in a generic solution
在通用解决方案中处理这些事情
- get rid of / or go to the last /
- if string has multiple dot('.') then only remove the string after last dot.
- 摆脱/或转到最后/
- 如果字符串有多个点('.'),则只删除最后一个点之后的字符串。