文件名中的井号 (#) 导致错误
我有一个非常简单的文件上传,允许用户上传 PDF 文件。然后,在另一页上,我通过锚标记引用这些文件。但是,当用户上传包含井号 (#) 的文件时,它似乎会破坏锚标记。它不会导致任何类型的 Coldfusion 错误,只是找不到该文件。如果我删除#,它就可以正常工作。我确信还有许多其他角色也会遇到同样的问题。
我尝试将 URLEncodedFormat()
放在锚点内的文件名周围,但这没有帮助。我唯一能想到的另一件事是在每次上传文件时重命名该文件并删除“#”字符(以及任何其他“坏”字符)。
必须有一个更简单的解决方案。有什么想法吗?
I have a very simple file upload that allows users to upload PDF files. On another page I then reference those files through an anchor tag. However, it seems that when a user upload a file that contains the pound sign (#) it breaks the anchor tag. It doesn't cause any type of Coldfusion error, it just can't find the file. If I remove the #, it works just fine. I am sure there are a number of other characters that would have this same issue.
I've tried putting URLEncodedFormat()
around the file name inside the anchor but that doesn't help. The only other thing I could think of was to rename the file each time it was uploaded and remove the "#" character (and any other "bad" character).
There has got to be an easier solution. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您控制文件上传代码,请尝试使用验证字符串,
否则,如果您对正则表达式感到满意,我会建议像以前的海报评论
这些示例假设您将添加“.pdf”并且只允许字母和数字。如果您需要下划线或句点,它看起来像这样...
我不是正则表达式专家,如果我有其中一个错误,我相信会有几个人跳出来纠正我:)
If you control the file upload code try validating the string with
Otherwise if you are comfortable with regex I would suggest something like the previous posters are commenting on
These samples assume you will add the ".pdf" and only allows letters and numbers. If you need underscores or the period it would look like this...
I am not a regex guru, if I have one of these wrong I am sure several will jump in and correct me :)
网络上的文件名中包含井号是不合法的。它们用于页内锚点目标:
因此,如果您有 file#name.pdf,浏览器实际上会查找文件“file”和内部锚点“name.pdf”。
是的,您必须在上传时重命名文件。
Pound signs are not legal within filenames on the web. They are used for in-page anchor targets:
So if you have file#name.pdf, the browser is actually looking for the file "file" and the internal anchor "name.pdf".
Yes, you will have to rename your files on upload.
我还不能发表评论
但凯文克的解决方案很好,除非您需要保留要替换的内容。
我们遇到了一个需要重命名文件名但需要保留文件名的实例(用户要求)。简单地删除特殊字符不是一个选择。因此,我们必须单独处理每个替换,类似的事情。
I can't comment yet,
but Kevink's solution is good unless you need to perserve what you're replacing.
We ran into an instance where we needed to rename the filename but the filename needed to be somewhat preserved (user requirement). Simply removing special characters wasn't an option. As a result we had to handle each replace individually, something like.
也许你必须用 ## 替换 # 才能避免这种情况,我认为这是因为 # 被认为是 Coldfusion 关键字。
Probably you would have to replace # with ## to avoid this, I think this is caused because # is figured as Coldfusion keyword.