如何使用 jQuery 文件上传来接受 .csv、.xls、.xlsx 文件?
How to accept .csv, .xsl, .xslx files using jQuery file upload?
我正在尝试使用它https://github.com/blueimp/jQuery-File-Upload
我已经用 PHP 实现了这个来管理上传的文件,一切正常,我想限制文件类型,上传者需要接受的是 .CSV、.XSL、.XSLX
我不知道如何配置它,因为没有关于接受这些扩展名的文件类型的解释。
@c25 的代码段
$('#fileupload').fileupload({
url: url,
dataType: 'json',
done: function (e, data) {
// Add each uploaded file name to the #files list
$.each(data.result.files, function (index, file) {
$('<li/>').text(file.name).appendTo('#files');
});
},
progressall: function (e, data) {
// Update the progress bar while files are being uploaded
var progress = parseInt(data.loaded / data.total * 100, 10);
$('#progress .bar').css(
'width',
progress + '%'
);
}
});
编辑:
我在 demo websites 来源上找到了以下内容:
$.widget('blueimp.fileupload', $.blueimp.fileupload, {
options: {
// The regular expression for allowed file types, matches
// against either file type or file name:
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
// The maximum allowed file size in bytes:
maxFileSize: 10000000, // 10 MB
// The minimum allowed file size in bytes:
minFileSize: undefined, // No minimal file size
// The limit of files to be uploaded:
maxNumberOfFiles: 10,
*/
// Function returning the current number of files,
// has to be overriden for maxNumberOfFiles validation:
getNumberOfFiles: $.noop,
// Error and info messages:
messages: {
maxNumberOfFiles: 'Maximum number of files exceeded',
acceptFileTypes: 'File type not allowed',
maxFileSize: 'File is too large',
minFileSize: 'File is too small'
}
},
可以找到选项的文档 here。
编辑
我发现基本插件还需要 2 个文件。 jquery.fileupload-process.js
和
jquery.fileupload-validate.js. Then modify the validate.js file to accept only the file extensions you need. The documentation for all this can be found here。 你真的应该阅读文档!
我认为这是执行 validation 的文件。它实际上扩展了主插件。
您可以使用以下代码行检查 PHP 中的文件类型:
function upload_file(){
$types = array('text/csv', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
if(in_array($_FILES['uploaded_filename']['type'], $types)){
//Manage the uploaded file here
} else {
//Handle the excluded filetypes here
}
在这里,您可以看到完整的 MIME 类型列表:http://www.freeformatter.com/mime-types-list.html
我正在尝试使用它https://github.com/blueimp/jQuery-File-Upload
我已经用 PHP 实现了这个来管理上传的文件,一切正常,我想限制文件类型,上传者需要接受的是 .CSV、.XSL、.XSLX
我不知道如何配置它,因为没有关于接受这些扩展名的文件类型的解释。
@c25 的代码段
$('#fileupload').fileupload({
url: url,
dataType: 'json',
done: function (e, data) {
// Add each uploaded file name to the #files list
$.each(data.result.files, function (index, file) {
$('<li/>').text(file.name).appendTo('#files');
});
},
progressall: function (e, data) {
// Update the progress bar while files are being uploaded
var progress = parseInt(data.loaded / data.total * 100, 10);
$('#progress .bar').css(
'width',
progress + '%'
);
}
});
编辑:
我在 demo websites 来源上找到了以下内容:
$.widget('blueimp.fileupload', $.blueimp.fileupload, {
options: {
// The regular expression for allowed file types, matches
// against either file type or file name:
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
// The maximum allowed file size in bytes:
maxFileSize: 10000000, // 10 MB
// The minimum allowed file size in bytes:
minFileSize: undefined, // No minimal file size
// The limit of files to be uploaded:
maxNumberOfFiles: 10,
*/
// Function returning the current number of files,
// has to be overriden for maxNumberOfFiles validation:
getNumberOfFiles: $.noop,
// Error and info messages:
messages: {
maxNumberOfFiles: 'Maximum number of files exceeded',
acceptFileTypes: 'File type not allowed',
maxFileSize: 'File is too large',
minFileSize: 'File is too small'
}
},
可以找到选项的文档 here。
编辑
我发现基本插件还需要 2 个文件。 jquery.fileupload-process.js 和 jquery.fileupload-validate.js. Then modify the validate.js file to accept only the file extensions you need. The documentation for all this can be found here。 你真的应该阅读文档!
我认为这是执行 validation 的文件。它实际上扩展了主插件。
您可以使用以下代码行检查 PHP 中的文件类型:
function upload_file(){
$types = array('text/csv', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
if(in_array($_FILES['uploaded_filename']['type'], $types)){
//Manage the uploaded file here
} else {
//Handle the excluded filetypes here
}
在这里,您可以看到完整的 MIME 类型列表:http://www.freeformatter.com/mime-types-list.html