我需要显示当前选定文件的名称(在<input type="file">
元素中)。
一切都很好,唯一的问题是我得到了这样的字符串"C:\fakepath \typog_rules.pdf“(browset自动将其作为输入元素的值)。
当我尝试按'\'
或'\\'
拆分字符串时,由于未转义的斜杠而失败。尝试匹配/替换斜杠也会失败。有什么办法可以解决这个问题吗?我需要它至少在Opera和IE中工作(因为在其他浏览器中我可以使用FileReader)
例如,我获取"C:\fakepath\typog_rules.pdf“作为输入,并希望获取"typog_rules.pdf”作为输出。
发布于 2013-01-19 01:32:59
我认为这更接近你想要的答案:
<input type="file">
$file = $(file);
var filename = fileElement[0].files[0].name;
发布于 2011-12-24 00:32:25
转义反斜杠字符。
foo.split('\\')
发布于 2018-07-02 19:10:17
有点老生常谈,但它是有效的:
const input = '\text';
const output = JSON.stringify(input).replace(/((^")|("$))/g, "").trim();
console.log({ input, output });
// { input: '\text', output: '\\text' }
https://stackoverflow.com/questions/8618374
复制相似问题