javascript - jQuery How to detect if file is being currently uploading (event is runing) -
i written code:
var fi = self.find('.file'); fi.on('change', function() { if(fi.prop('files').length === 1) { var file = fi.prop('files')[0], name = file.name, ext = name.split('.').pop().tolowercase(), size = (file.size / 1048576).tofixed(2), type = file.type; if($.inarray(ext, ['jpg', 'png', 'gif']) == -1) { return false; } if($.inarray(type, ['image/jpeg', 'image/png', 'image/gif']) == -1) { return false; } if(size > 1.2) { return false; } ajax(createdata(file)); return false; } return false; });
i allowing upload 1 file @ time ( html side, js side , php side of view ), script if user selects bigger size file, lets 2+ mb , fast clicker can select file , creates second event , both files being uploaded server causes chaos , problems. implementing drag , drop upload option beginner javascript coder im asking community if there way to:
prevent triggering script while file being uploaded ( dont mean methods hide input or diable or take off event listener while, mean method detects if event running , prevent executing script ). tried things default prevent or stop propagation these dont work obviously.
adding button cancel on going script ( meaning stop file uploading , "clean up" user can safetly try again )
thank in advance hints , guidance ;)
i worked on 1 of side projects. here's code sample:
var filereader = new filereader(); var filefilter = /^(?:image\/bmp|image\/cis\-cod|image\/gif|image\/ief|image\/jpeg|image\/jpeg|image\/jpeg|image\/pipeg|image\/png|image\/svg\+xml|image\/tiff|image\/x\-cmu\-raster|image\/x\-cmx|image\/x\-icon|image\/x\-portable\-anymap|image\/x\-portable\-bitmap|image\/x\-portable\-graymap|image\/x\-portable\-pixmap|image\/x\-rgb|image\/x\-xbitmap|image\/x\-xpixmap|image\/x\-xwindowdump)$/i; var imageobject = { valid: false }; filereader.onload = function (filereaderevent) { imageobject.data = filereaderevent.target.result; }; var loadimage = function() { if (element[0].files.length === 0) { return; } var file = element[0].files[0]; imageobject.filename = file.name; if (!filefilter.test(file.type)) { imageobject.error = 'you must select valid image!'; imageobject.valid = false; return; }else{ imageobject.error = ''; imageobject.valid = true; } filereader.readasdataurl(file); }; element.on('change', loadimage); }
this angularjs project i've removed angular components should pretty easy add in necessary jquery code.
Comments
Post a Comment