html 文件上传校验,上传文件校验格式

function fileChange(target) {

var name=target.value;

var fileName = name.substring(name.lastIndexOf(".")+1).toLowerCase();

if(fileName !=="doc" && fileName !=="docx" && fileName !== ‘xlsx‘ && fileName !== ‘xls‘ && fileName !== ‘jpg‘ && fileName !==‘png‘){

toastr.error("请选择doc/docx/xlsx/xls等格式的文件");

target.value="";

return false;   //阻止submit提交

}

}

(function($) {

$.fn.ajaxfileupload = function(options) {

var settings = {

params: {},

action: ‘‘,

onStart: function() { },

onComplete: function(response) { },

onCancel: function() { },

validate_extensions : true,

valid_extensions : [‘doc‘,‘docx‘,‘xlsx‘,‘xls‘,‘jpg‘,‘png‘],

submit_button : null

};

var uploading_file = false;

if ( options ) {

$.extend( settings, options );

}

// ‘this‘ is a jQuery collection of one or more (hopefully)

// file elements, but doesn‘t check for this yet

return this.each(function() {

var $element = $(this);

// Skip elements that are already setup. May replace this

// with uninit() later, to allow updating that settings

if($element.data(‘ajaxUploader-setup‘) === true) return;

$element.change(function()

{

// since a new image was selected, reset the marker

uploading_file = false;

// only update the file from here if we haven‘t assigned a submit button

if (settings.submit_button == null)

{

upload_file();

}

});

if (settings.submit_button == null)

{

// do nothing

} else

{

settings.submit_button.click(function(e)

{

// Prevent non-AJAXy submit

e.preventDefault();

// only attempt to upload file if we‘re not uploading

if (!uploading_file)

{

upload_file();

}

});

}

var upload_file = function()

{

if($element.val() == ‘‘) return settings.onCancel.apply($element, [settings.params]);

// make sure extension is valid

var ext = $element.val().split(‘.‘).pop().toLowerCase();

if(true === settings.validate_extensions && $.inArray(ext, settings.valid_extensions) == -1)

{

// Pass back to the user

settings.onComplete.apply($element, [{status: false, message: ‘The select file type is invalid. File must be ‘ + settings.valid_extensions.join(‘, ‘) + ‘.‘}, settings.params]);

} else

{

uploading_file = true;

// Creates the form, extra inputs and iframe used to

// submit / upload the file

wrapElement($element);

// Call user-supplied (or default) onStart(), setting

// it‘s this context to the file DOM element

var ret = settings.onStart.apply($element, [settings.params]);

// let onStart have the option to cancel the upload

if(ret !== false)

{

$element.parent(‘form‘).submit(function(e) { e.stopPropagation(); }).submit();

} else {

uploading_file = false;

}

}

};

// Mark this element as setup

$element.data(‘ajaxUploader-setup‘, true);

/*

// Internal handler that tries to parse the response

// and clean up after ourselves.

*/

var handleResponse = function(loadedFrame, element) {

var response, responseStr = $(loadedFrame).contents().text();

try {

//response = $.parseJSON($.trim(responseStr));

response = JSON.parse(responseStr);

} catch(e) {

response = responseStr;

}

// Tear-down the wrapper form

element.siblings().remove();

element.unwrap();

uploading_file = false;

// Pass back to the user

settings.onComplete.apply(element, [response, settings.params]);

};

/*

// Wraps element in a

tag, and inserts hidden inputs for each

// key:value pair in settings.params so they can be sent along with

// the upload. Then, creates an iframe that the whole thing is

// uploaded through.

*/

var wrapElement = function(element) {

// Create an iframe to submit through, using a semi-unique ID

var frame_id = ‘ajaxUploader-iframe-‘ + Math.round(new Date().getTime() / 1000)

$(‘body‘).after(‘‘);

$(‘#‘+frame_id).get(0).onload = function() {

handleResponse(this, element);

};

// Wrap it in a form

element.wrap(function() {

return ‘

})

// Insert ‘s for each param

.before(function() {

var key, html = ‘‘;

for(key in settings.params) {

var paramVal = settings.params[key];

if (typeof paramVal === ‘function‘) {

paramVal = paramVal();

}

html += ‘‘;

}

return html;

});

}

});

}

})( jQuery )

$(‘#analysisLogFile‘).ajaxfileupload({

action: ‘/upload_log_object‘,

valid_extensions : [‘doc‘,‘docx‘,‘xlsx‘,‘xls‘,‘jpg‘,‘png‘],

params: {

extra: ‘info‘

},

onComplete: function(response) {

$("#analysisLogFilePath").attr("value",response.filePath);

},

onStart: function() {

return true; // cancels upload

},

onCancel: function() {

console.log(‘no file selected‘);

}

});

原文:https://www.cnblogs.com/michaelcnblogs/p/12402013.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值