非ASCII字符仅在Safari浏览器中添加表单输入 [英] Non-ascii characters added form input only with Safari Browser

查看:194
本文介绍了非ASCII字符仅在Safari浏览器中添加表单输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个基于jQuery的HTML5拖放式上传表单。它适用于除Safari以外的所有浏览器,对于具有特定扩展名的文件,文件扩展名之后的文件名将用非ASCII字符填充。



example.mov 变成 example.movçðÆê



此外,文件已损坏:他们似乎没有内容。

这是Safari和jQuery / HTML5的一个已知问题吗?有没有什么办法来过滤非ASCII字符?

解决方案

我不确定这是否有用,就像Pekka,我不是100%的情况,但是如果足够从字符串中去掉错误的字符,那么你可以使用正则表达式。这个将删除所有不是 az , AZ 0-9

  filename.replace(/ [^ a-z0-9 \。。] + / gi,); 

这可能太严格了(比如你想允许非英文的文件名,或者你只想要去掉扩展后的字符)。假设问题出在mov和pdf的扩展名上,你只想从扩展名的末尾删除上面的字符,你可以使用

  filename.replace(/(\.mov | \.pdf)[^ a-z0-9 \。] + $ / i,$ 1); 


I'm facing a weird problem with the Safari browser for Windows.

I have an HTML5 drag-n-drop upload form based on jQuery. It works fine with all the browsers except Safari where, for files with specific extensions, the file name is padded with non-ascii characters after the file extension.

E.g., file example.mov becomes example.movçðÆê

Also the files are corrupted: they seem to have no content.

Is this a known issue with Safari and jQuery/HTML5? Is there any way to filter-out non-ascii characters?

解决方案

I'm not sure if this is useful as, like Pekka, I'm not 100% on the situation here, but if it's enough to strip 'wrong' characters from a string then you could use a regex. This one will remove any characters that aren't a-z, A-Z, 0-9 or ..

filename.replace(/[^a-z0-9\.]+/gi, "");

That may be too restrictive (e.g. you want to allow non-English-like filenames or you only want to strip characters after the extension). Assuming the problem is with the mov and pdf extensions and you only want to remove characters as above from the end of the extension, you could use

filename.replace(/(\.mov|\.pdf)[^a-z0-9\.]+$/i, "$1");

这篇关于非ASCII字符仅在Safari浏览器中添加表单输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆