1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | < html > < head > < meta content = "text/html; charset=utf-8" http-equiv = content -type> < title >Test</ title > < style type = "text/css" > .photoUpload{ border: 10px dashed #808080; width: 800px; height: 300px; float: left; padding: 10px; } .photoUpload img{ margin-left: 10px; } .dashboard_target_box.over { border:3px dashed #000; background:#ffa } </ style > < script src = "js/jquery-1.9.1.js" ></ script > < script src = "js/multiple-textarea.js" ></ script > </ head > < body > < div class = "photoUpload dashboard_target_box" contenteditable = "true" id = "photoUploadId" ></ div > < div >< input id = "fileInputId" type = "file" multiple = "multiple" ></ div > < script type = "text/javascript" > $('.photoUpload').createMultiple({ fileContainer:'photoUploadId', inputfile:'fileInputId', height:50, width:50 }) </ script > </ body > </ html > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | /*** * author:YuHongDa * date:2015-08-19 * div contenteditable+input.file 实现拖拽传送文件(暂时支持单个文件拖拽,多个待实现) * 此demo未考虑浏览器兼容性,测试过Chrome和FF * @defaults * fileContainer:存放文件容器Id * height:缩略图高度 * width:缩略图宽度 * inputfile:上传文件组件的ID, * filesize:上传文件大小 * fileType:上传文件分类 1:图片 2:其他文件(暂未实现) (其他待实现) * photofiles:上传图片后缀名,此属性与fileType共同使用,fileType为1时生效,其他时可忽略 * otherfiles:其他文件后缀名,此属性与fileType共同使用,fileType为2时生效,其他时可忽略 * * */ ( function ($, doc, undefined) { $.fn.createMultiple = function (params) { var defaults = { fileContainer: '' , height: 20, width: 20, inputfile: '' , filesize: 200, filetype: 1, photofiles: [ 'png' ], otherfiles: [] }; var options = $.extend(defaults, params); if (options.fileContainer == "" ) { throw new Error( "The style is Empty!" ); } if (options.inputfile == "" ) { throw new Error( "The inputFile Id is Empty!" ); } if ( typeof options.filetype != "number" ) { throw new Error( "The file type must be Number And can't be empty!" ); } var fileContainer = $(' #' + options.fileContainer), inputFile = doc.getElementById(options.inputfile), fileTempLate = "<img src={0} height={1} width={2}>", emum = { photo: 1, other: 2 }; /***type file is Photos***/ var showPhoto = function () { if (inputFile.files) { appendImage(inputFile.files); } }, /***types file is others***/ showOther = function () { alert( "to be continued!" ) }, appendImage = function (files) { var sizeStr= "" ,typeStr= "" ; for ( var i = 0, len = files.length; i < len; i++) { if (files[i].size / 1000 > options.filesize) { sizeStr+=files[i].name; sizeStr+= "、" ; continue ; } if (options.photofiles.indexOf(files[i].name.substring(files[i].name.lastIndexOf('. ') + 1, files[i].name.length)) == -1) { typeStr+=files[i].name; typeStr+="、"; continue; } var reader = new FileReader(); reader.readAsDataURL(files[i]); reader.onload = function (e) { var img = this.result; fileContainer.html(fileContainer.html() + String.imgFormat(fileTempLate, img, options.height, options.width)); } } if(sizeStr){ alert(sizeStr+" are too large,Must be smaller than ‘" + options.filesize + "KB’"); } if(typeStr){ alert(' Does is not support this kind of type: '+typeStr); } } $(' #' + options.inputfile).bind('change', function () { switch (options.filetype) { case emum.photo: showPhoto(); break ; case emum.other: break ; showOther(); default : alert( "The fileType is undefined!" ); } }); $(document).on({ dragleave: function (e) { e.preventDefault(); fileContainer.removeClass('over '); }, drop: function (e) { e.preventDefault(); fileContainer.removeClass(' over '); }, dragenter: function (e) { e.preventDefault(); fileContainer.addClass(' over '); }, dragover: function (e) { e.preventDefault(); fileContainer.addClass(' over '); } }); doc.getElementById(options.fileContainer).addEventListener(' drop', function (e) { e.preventDefault(); if (e.dataTransfer.files) { appendImage(e.dataTransfer.files); } }, false ); } })(window.jQuery, window.document); String.imgFormat = function (str) { for ( var i = 1; i < arguments.length; i++) { str = str.replace( new RegExp( "\\{" + (i - 1) + "\\}" , "g" ), arguments[i] != undefined ? arguments[i] : "" ); } return str; }; |
特别申明:
本站所有资源都是由网友投稿发布,或转载各大下载站,请自行检测软件的完整性!
本站所有资源仅供学习与参考,请勿用于商业用途,否则产生的一切后果将由您自己承担!
如有侵权请联系我们删除下架,联系方式:lei1294551502@163.com