javascript 在javascript中验证除扩展名之外的pdf文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10309857/
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
Validating a pdf file other than its extension in javascript?
提问by suraj
I give user the option of uploading a file like this
我给用户上传这样的文件的选项
<form action="#" onsubmit="return Checkfiles(this);">
<center><input type="file" id="filename">
<input type="submit" value="Go!"></center>
</form>
When user uploads the file, I validate the file using the following javascript function
当用户上传文件时,我使用以下 javascript 函数验证文件
<script type="text/javascript">
function Checkfiles()
{
var fup = document.getElementById('filename');
var fileName = fup.value;
var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
if(ext == "pdf" )
{
return true;
}
else
{
alert("Upload pdf files only");
fup.focus();
return false;
}
}
</script>
Evertything is working fine with it.
But I want to validate file by some other means and not just by its extension
I will give reason for doing this. I renamed an image file to image.pdf and now its in pdf format but couldnt be opened.
一切都很好。
但我想通过其他方式验证文件,而不仅仅是通过其扩展名,
我将给出这样做的理由。我将一个图像文件重命名为 image.pdf,现在它是 pdf 格式,但无法打开。
So I want to validate a pdf file in exact way. Is there any other way to check other than by its extension?
Edit
I want to do validation at server end using jsp page. So what should I do for that?
Thanks in ADVANCE :)
所以我想以确切的方式验证 pdf 文件。除了通过扩展名之外,还有其他方法可以检查吗?
编辑
我想在服务器端使用 jsp 页面进行验证。那我应该怎么做呢?
提前致谢 :)
采纳答案by Tom
As far as I know, checking if a pdf actually is a pdf can only be done on server side. You could try and open it with IText or something similar; I bet it throws some sort of exception when you try opening or modifying something else then a PDF.
据我所知,检查 pdf 是否真的是 pdf 只能在服务器端完成。你可以尝试用 IText 或类似的东西打开它;我敢打赌,当您尝试打开或修改 PDF 之外的其他内容时,它会引发某种异常。
回答by Māris Kise?ovs
Any kind of javascript validation is only for users comfort. Everything should be validated on serverside too. User can easily disable or modify any javascript code.
任何类型的 javascript 验证都只是为了让用户感到舒适。一切都应该在服务器端进行验证。用户可以轻松禁用或修改任何 javascript 代码。
Anyway. you can't check file contents in javascript.
反正。您无法在 javascript 中检查文件内容。
In PHP, you can use http://www.php.net/manual/en/function.finfo-file.phpto detect file type by it's contents. Most of other languages should have similar functionality.
在 PHP 中,您可以使用http://www.php.net/manual/en/function.finfo-file.php通过其内容检测文件类型。大多数其他语言应该具有类似的功能。
回答by Florian Margaine
To validate a filetype on javascript, here are some ways:
要在 javascript 上验证文件类型,有以下几种方法:
// Your way: getting the extension and checking it.
// I suggest this way instead of using indexOf:
var ext = fileName.split('.').reverse()[0]
// Splits by '.', reverse the array, and returns the first element
// Another way: checking the type of the file
if ( fup.files[0].type === 'application/pdf' ) {
console.log( 'It is validated!' )
}
Live example: http://jsbin.com/akati3/2thanks to https://stackoverflow.com/a/4581316/851498
现场示例:http: //jsbin.com/akati3/2感谢https://stackoverflow.com/a/4581316/851498
But then, you should reallyvalidate this on the server side as others said.
但是,您真的应该像其他人所说的那样在服务器端验证这一点。