如何上传仅PDF文件 [英] How to upload pdf file only
本文介绍了如何上传仅PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要上传PDF格式不仅没有doc文件,如果我使用JavaScript code它将接受doc文件
还
函数CheckFile()
{
var文件=的document.getElementById('FileUpload1');
VAR LEN = file.value.length;
VAR EXT = file.value;
如果(file.value.length&下; = 0)
{
警报('请选择要导入的文件');
的document.getElementById('FileUpload1')专注()。
返回false;
}
否则,如果(ext.substr(LEN-3,LEN)!=PDF)
{
警报(请选择一个PDF文件);
返回false;
}
}
解决方案
有没有prefect解决方案,以验证PDF文件,但你可以做的是(在服务器端):
- 验证扩展(PDF,DOC,DOCX)*几乎无用
- 验证 MIME
- 开启PDF文件,读取头(第一行),并检查它是否包含
这些字符串之一:%PDF-1.0%PDF-1.1%PDF-1.2,PDF%-1.3,
%PDF-1.4
作为一个额外的我也检查,如果该文件包含
字符串通过搜索多个指定的页数
/页
I want to upload pdf only not doc file if i use javascript code it will accept doc file also
function CheckFile()
{
var file = document.getElementById('FileUpload1');
var len=file.value.length;
var ext=file.value;
if (file.value.length <= 0)
{
alert('Please select a file to import');
document.getElementById('FileUpload1').focus();
return false;
}
else if (ext.substr(len-3,len)!="pdf" )
{
alert("Please select a pdf file ");
return false;
}
}
解决方案
There is no prefect solution to validate PDF file but what you can do is (on the server-side):
- Validate extensions (pdf, doc, docx) *almost useless
- Validate MIME
- Open PDF file, read the header (first line) and check if it contains one of these strings: %PDF-1.0, %PDF-1.1, %PDF-1.2, %PDF-1.3, %PDF-1.4
As an extra I also check if the file contains a string that specifies the number of pages by searching for multiple "/Page"
这篇关于如何上传仅PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文