问题
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?
回答1:
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");
来源:https://stackoverflow.com/questions/7747271/non-ascii-characters-added-form-input-only-with-safari-browser