前端多文件拖拽上传(可上传文件夹)

html代码:

<form action="lib/winui/js/dataTest.json" class="dropzone needsclick dz-clickable" id="demo-upload">
    <div class="dz-message needsclick" id="myId"></div>
</form>

js代码:

$(function () {
            (function () {
                var a, b, c, d, e, f, g, h, i, j = [].slice, k = function (a, b) {
                    function c() {
                        this.constructor = a;
                    }

                    for (var d in b) l.call(b, d) && (a[d] = b[d]);
                    return c.prototype = b.prototype, a.prototype = new c, a.__super__ = b.prototype, a
                }, l = {}.hasOwnProperty;
                h = function () {
                }, b = function () {
                    function a() {
                    }

                    return a.prototype.addEventListener = a.prototype.on, a.prototype.on = function (a, b) {
                        return this._callbacks = this._callbacks || {}, this._callbacks[a] || (this._callbacks[a] = []), this._callbacks[a].push(b), this
                    }, a.prototype.emit = function () {
                        var a, b, c, d, e, f;
                        if (d = arguments[0], a = 2 <= arguments.length ? j.call(arguments, 1) : [], this._callbacks = this._callbacks || {}, c = this._callbacks[d]) for (e = 0, f = c.length; e < f; e++) b = c[e], b.apply(this, a);
                        return this
                    }, a.prototype.removeListener = a.prototype.off, a.prototype.removeAllListeners = a.prototype.off, a.prototype.removeEventListener = a.prototype.off, a.prototype.off = function (a, b) {
                        var c, d, e, f;
                        if (!this._callbacks || 0 === arguments.length) return this._callbacks = {}, this;
                        if (!(c = this._callbacks[a])) return this;
                        if (1 === arguments.length) return delete this._callbacks[a], this;
                        for (d = e = 0, f = c.length; e < f; d = ++e) if (c[d] === b) {
                            c.splice(d, 1);
                            break
                        }
                        return this
                    }, a
                }(), a = function (a) {
                    function d(a, b) {
                        var c, f, g;
                        if (this.element = a, this.version = d.version, this.defaultOptions.previewTemplate = this.defaultOptions.previewTemplate.replace(/\n*/g, ""), this.clickableElements = [], this.listeners = [], this.files = [], "string" == typeof this.element && (this.element = document.querySelector(this.element)), !this.element || null == this.element.nodeType) throw new Error("Invalid dropzone element.");
                        if (this.element.dropzone) throw new Error("Dropzone already attached.");
                        if (d.instances.push(this), this.element.dropzone = this, c = null != (g = d.optionsForElement(this.element)) ? g : {}, this.options = e({}, this.defaultOptions, c, null != b ? b : {}), this.options.forceFallback || !d.isBrowserSupported()) return this.options.fallback.call(this);
                        if (null == this.options.url && (this.options.url = this.element.getAttribute("action")), !this.options.url) throw new Error("No URL provided.");
                        if (this.options.acceptedFiles && this.options.acceptedMimeTypes) throw new Error("You can't provide both 'acceptedFiles' and 'acceptedMimeTypes'. 'acceptedMimeTypes' is deprecated.");
                        this.options.acceptedMimeTypes && (this.options.acceptedFiles = this.options.acceptedMimeTypes, delete this.options.acceptedMimeTypes), null != this.options.renameFilename && (this.options.renameFile = function (a) {
                            return function (b) {
                                return a.options.renameFilename.call(a, b.name, b)
                            }
                        }(this)), this.options.method = this.options.method.toUpperCase(), (f = this.getExistingFallback()) && f.parentNode && f.parentNode.removeChild(f), !1 !== this.options.previewsContainer && (this.options.previewsContainer ? this.previewsContainer = d.getElement(this.options.previewsContainer, "previewsContainer") : this.previewsContainer = this.element), this.options.clickable && (!0 === this.options.clickable ? this.clickableElements = [this.element] : this.clickableElements = d.getElements(this.options.clickable, "clickable")), this.init()
                    }

                    var e, f;
                    return k(d, a), d.prototype.Emitter = b, d.prototype.events = ["drop", "dragstart", "dragend", "dragenter", "dragover", "dragleave", "addedfile", "addedfiles", "removedfile", "thumbnail", "error", "errormultiple", "processing", "processingmultiple", "uploadprogress", "totaluploadprogress", "sending", "sendingmultiple", "success", "successmultiple", "canceled", "canceledmultiple", "complete", "completemultiple", "reset", "maxfilesexceeded", "maxfilesreached", "queuecomplete"], d.prototype.defaultOptions = {
                        url: null,
                        method: "post",
                        withCredentials: !1,
                        timeout: 3e4,
                        parallelUploads: 2,
                        uploadMultiple: !1,
                        maxFilesize: 256,
                        paramName: "file",
                        createImageThumbnails: !0,
                        maxThumbnailFilesize: 10,
                        thumbnailWidth: 120,
                        thumbnailHeight: 120,
                        thumbnailMethod: "crop",
                        resizeWidth: null,
                        resizeHeight: null,
                        resizeMimeType: null,
                        resizeQuality: .8,
                        resizeMethod: "contain",
                        filesizeBase: 1e3,
                        maxFiles: null,
                        params: {},
                        headers: null,
                        clickable: !0,
                        ignoreHiddenFiles: !0,
                        acceptedFiles: null,
                        acceptedMimeTypes: null,
                        autoProcessQueue: !0,
                        autoQueue: !0,
                        addRemoveLinks: !1,
                        previewsContainer: null,
                        hiddenInputContainer: "body",
                        capture: null,
                        renameFilename: null,
                        renameFile: null,
                        forceFallback: !1,
                        dictDefaultMessage: "Drop files here to upload",
                        dictFallbackMessage: "Your browser does not support drag'n'drop file uploads.",
                        dictFallbackText: "Please use the fallback form below to upload your files like in the olden days.",
                        dictFileTooBig: "File is too big ({
  {filesize}}MiB). Max filesize: {
  {maxFilesize}}MiB.",
                        dictInvalidFileType: "You can't upload files of this type.",
                        dictResponseError: "Server responded with {
  {statusCode}} code.",
                        dictCancelUpload: "Cancel upload",
                        dictCancelUploadConfirmation: "Are you sure you want to cancel this upload?",
                        dictRemoveFile: "Remove file",
                        dictRemoveFileConfirmation: null,
                        dictMaxFilesExceeded: "You can not upload any more files.",
                        dictFileSizeUnits: {tb: "TB", gb: "GB", mb: "MB", kb: "KB", b: "b"},
                        init: function () {
                            return h
                        },
                        accept: function (a, b) {
                            return b()
                        },
                        fallback: function () {
                            var a, b, c, e, f, g;
                            for (this.element.className = this.element.className + " dz-browser-not-supported", f = this.element.getElementsByTagName("div"), b = 0, c = f.length; b < c; b++) a = f[b], /(^| )dz-message($| )/.test(a.className) && (e = a, a.className = "dz-message");
                            return e || (e = d.createElement('<div class="dz-message"><span></span></div>'), this.element.appendChild(e)), g = e.getElementsByTagName("span")[0], g && (null != g.textContent ? g.textContent = this.options.dictFallbackMessage : null != g.innerText && (g.innerText = this.options.dictFallbackMessage)), this.element.appendChild(this.getFallbackForm())
                        },
                        resize: function (a, b, c, d) {
                            var e, f, g;
                            if (e = {
                                srcX: 0,
                                srcY: 0,
                                srcWidth: a.width,
                                srcHeight: a.height
                            }, f = a.width / a.height, null == b && null == c ? (b = e.srcWidth, c = e.srcHeight) : null == b ? b = c * f : null == c && (c = b / f), b = Math.min(b, e.srcWidth), c = Math.min(c, e.srcHeight), g = b / c, e.srcWidth > b || e.srcHeight > c) if ("crop" === d) f > g ? (e.srcHeight = a.height, e.srcWidth = e.srcHeight * g) : (e.srcWidth = a.width, e.srcHeight = e.srcWidth / g); else {
                                if ("contain" !== d) throw new Error("Unknown resizeMethod '" + d + "'");
                                f > g ? c = b / f : b = c * f
                            }
                            return e.srcX = (a.width - e.srcWidth) / 2, e.srcY = (a.height - e.srcHeight) / 2, e.trgWidth = b, e.trgHeight = c, e
                        },
                        transformFile: function (a, b) {
                            return (this.options.resizeWidth || this.options.resizeHeight) && a.type.match(/image.*/) ? this.resizeImage(a, this.options.resizeWidth, this.options.resizeHeight, this.options.resizeMethod, b) : b(a)
                        },
                        previewTemplate: '<div class="dz-preview dz-file-preview">\n  <div class="dz-image"><img data-dz-thumbnail /></div>\n  <div class="dz-details">\n    <div class="dz-size"><span data-dz-size></span></div>\n    <div class="dz-filename"><span data-dz-name></span></div>\n  </div>\n  <div class="dz-progress"><span class="dz-upload" data-dz-uploadprogress></span></div>\n  <div class="dz-error-message"><span data-dz-errormessage></span></div>\n  <div class="dz-success-mark">\n    <svg width="54px" height="54px" viewBox="0 0 54 54" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:sketch="http://www.bohemiancoding.com/sketch/ns">\n      <title>Check</title>\n      <defs></defs>\n      <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" sketch:type="MSPage">\n        <path d="M23.5,31.8431458 L17.5852419,25.9283877 C16.0248253,24.3679711 13.4910294,24.366835 11.9289322,25.9289322 C10.3700136,27.4878508 10.3665912,30.0234455 11.9283877,31.5852419 L20.4147581,40.0716123 C20.5133999,40.1702541 20.6159315,40.2626649 20.7218615,40.3488435 C22.2835669,41.8725651 24.794234,41.8626202 26.3461564,40.3106978 L43.3106978,23.3461564 C44.8771021,21.7797521 44.8758057,19.2483887 43.3137085,17.6862915 C41.7547899,16.1273729 39.2176035,16.1255422 37.6538436,17.6893022 L23.5,31.8431458 Z M27,53 C41.3594035,53 53,41.3594035 53,27 C53,12.6405965 41.3594035,1 27,1 C12.6405965,1 1,12.6405965 1,27 C1,41.3594035 12.6405965,53 27,53 Z" id="Oval-2" stroke-opacity="0.198794158" stroke="#747474" fill-opacity="0.816519475" fill="#FFFFFF" sketch:type="MSShapeGroup"></path>\n      </g>\n    </svg>\n  </div>\n  <div class="dz-error-mark">\n    <svg width="54px" height="54px" viewBox="0 0 54 54" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:sketch="http://www.bohemiancoding.com/sketch/ns">\n      <title>Error</title>\n      <defs></defs>\n      <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" sketch:type="MSPage">\n        <g id="Check-+-Oval-2" sketch:type="MSLayerGroup" stroke="#747474" stroke-opacity="0.198794158" fill="#FFFFFF" fill-opacity="0.816519475">\n          <path d="M32.6568542,29 L38.3106978,23.3461564 C39.8771021,21.7797521 39.8758057,19.2483887 38.3137085,17.6862915 C36.7547899,16.1273729 34.2176035,16.1255422 32.6538436,17.6893022 L27,23.3431458 L21.3461564,17.6893022 C19.7823965,16.1255422 17.2452101,16.1273729 15.6862915,17.6862915 C14.1241943,19.2483887 14.1228979,21.7797521 15.6893022,23.3461564 L21.3431458,29 L15.6893022,34.6538436 C14.1228979,36.2202479 14.1241943,38.7516113 15.6862915,40.3137085 C17.2452101,41.8726271 19.7823965,41.8744578 21.3461564,40.3106978 L27,34.6568542 L32.6538436,40.3106978 C34.2176035,41.8744578 36.7547899,41.8726271 38.3137085,40.3137085 C39.8758057,38.7516113 39.8771021,36.2202479 38.3106978,34.6538436 L32.6568542,29 Z M27,53 C41.3594035,53 53,41.3594035 53,27 C53,12.6405965 41.3594035,1 27,1 C12.6405965,1 1,12.6405965 1,27 C1,41.3594035 12.6405965,53 27,53 Z" id="Oval-2" sketch:type="MSShapeGroup"></path>\n        </g>\n      </g>\n    </svg>\n  </div>\n</div>',
                        drop: function (a) {
                            return this.element.classList.remove("dz-drag-hover")
                        },
                        dragstart: h,
                        dragend: function (a) {
                            return this.element.classList.remove("dz-drag-hover")
                        },
                        dragenter: function (a) {
                            return this.element.classList.add("dz-drag-hover")
                        },
                        dragover: function (a) {
                            return this.element.classList.add("dz-drag-hover")
                        },
                        dragleave: function (a) {
                            return this.element.classList.remove("dz-drag-hover")
                        },
                        paste: h,
                        reset: function () {
                            return this.element.classList.remove("dz-started")
                        },
                        addedfile: function (a) {
                            var b, c, e, f, g, h, i, j, k, l, m, n, o;
                            if (this.element === this.previewsContainer && this.element.classList.add("dz-started"), this.previewsContainer) {
                                for (a.previewElement = d.createElement(this.options.previewTemplate.trim()), a.previewTemplate = a.previewElement, this.previewsContainer.appendChild(a.previewE
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
前端实现iOS拖拽上传文件可以通过使用HTML5中的拖放API来实现。以下是基本的实现步骤: 1. 创建一个<div id="drop-area">元素,用于接受拖拽上传文件。 2. 监听该元素的dragover和drop事件,分别处理文件拖拽文件上传操作。 3. 在dragover事件处理函数中,阻止浏览器默认的拖拽行为。 4. 在drop事件处理函数中,获取拖拽上传文件中的所有文件。 5. 将文件转换成FormData对象,并使用XMLHttpRequest对象将其发送到服务器。 下面是一个简单的示例代码,用于拖拽上传文件: ```html <div id="drop-area">将文件拖拽到此处上传</div> <script> var dropArea = document.getElementById('drop-area'); dropArea.addEventListener('dragover', function(event) { event.preventDefault(); dropArea.classList.add('drag-over'); }); dropArea.addEventListener('dragleave', function(event) { dropArea.classList.remove('drag-over'); }); dropArea.addEventListener('drop', function(event) { event.preventDefault(); dropArea.classList.remove('drag-over'); var files = event.dataTransfer.items; if (!files.length) { alert('请选择文件!'); return; } var formData = new FormData(); for (var i = 0; i < files.length; i++) { if (files[i].kind === 'file') { var file = files[i].getAsFile(); formData.append('files[]', file); } } var xhr = new XMLHttpRequest(); xhr.open('POST', '/upload', true); xhr.onload = function() { if (xhr.status === 200) { alert('上传成功!'); } else { alert('上传失败!'); } }; xhr.send(formData); }); </script> ``` 需要注意的是,拖拽上传文件时需要将文件下的所有文件转换成FormData对象,并使用XMLHttpRequest对象将其发送到服务器。在示例代码中,我使用了FormData对象的append方法来添加文件到表单中,使用XMLHttpRequest对象的open和send方法将表单发送到服务器。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值