F11*_*F11 7 javascript asp.net asp.net-mvc jquery drag-and-drop
我在执行拖放文件一样的这在asp.net mvc的5,但我的要求是,当我拖动文件,它不应该被立即上传.首先拖动文件,然后单击一个按钮("输入MetaData")为每个文件输入几个必需属性(元数据名称,类别等等),然后单击其他按钮(提交)以提交上传.
通常当我们拖动文件时,它会立即上传,我必须停止它并按下按钮(在填充其他字段之后).甚至任何具有类似功能的第三方js库?
我google了很多但没有得到预期的结果.有人可以指导我如何满足此要求或提供一些链接来满足此要求.
您链接的示例代码似乎使用了由Weixi Yen编写的jquery.filedrop.js.您需要从项目主页下载并使用最新版本才能使用.
您还应该下载并使用比该示例代码捆绑的更高版本的jquery.我用jquery 1.9.1测试了这个.
要使用您选择的jquery扩展,您需要利用该beforeSend选项,并提供您自己的功能.您还需要存储done()对每个文件提供给自定义函数的函数的引用,以便稍后调用它们,从而导致文件上载.
如果您希望为每个文件显示元框,则需要在每个文件的基础上附加相应的html以允许用户填写它们.
我建议的代码摘要如下:
var uploads_to_call = []; // the global to store all the file upload callbacks
$('#dropzone').filedrop({
fallback_id: 'upload_button', // an identifier of a standard file input element, becomes the target of "click" events on the dropzone
url: 'upload.php', // upload handler, handles each file separately, can also be a function taking the file and returning a url
// other important parameters related to upload, read the documentation for details
// this is the important custom function you need to supply
beforeSend: function(file, i, done) {
// file is a file object
// i is the file index
// call done() to start the upload
// this is just to clone meta boxes for the user to fill in for each file
// it also fills in the filename so that it's obvious which meta boxes
// are for which files
$("#perfile").children().clone()
.appendTo("#allmeta")
.children(".filename").html(file.name);
// this pushes the done() callback into the global mentioned earlier
// so that we can call it later
uploads_to_call.push(done);
},
afterAll: function() {
// runs after all files have been uploaded or otherwise dealt with
// you should possibly put code in here to clean up afterwards
}
});
// set a handler to upload the files when the submit button is clicked
$("#submit").click(function(){
$.each(uploads_to_call, function(i, upcall) {
upcall();
});
});
Run Code Online (Sandbox Code Playgroud)
使用类似于以下内容的html:
<form>
<div id="dropzone"></div>
<div id="allmeta"></div>
<button id="submit">submit</button>
</form>
<div id="perfile">
<div class="meta">
<span class="filename"></span>
<input type="text" placeholder="meta"/>
<input type="text" placeholder="meta"/>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
本div#perfile应具有的CSS来隐藏它,因为它只是用来包含股利每一个文件被拖动时间被克隆到表单中.
我在这里创建了一个概念证明jsfiddle,显然这实际上并不允许上传文件,但它显示了JS方面的工作.你需要向下滚动到javascript面板的底部才能看到自定义代码 - 顶部的东西只包括你应该从项目主页网站下载的javascript扩展.
这应该足以让你让asp.net方面的工作正常运行.您只需要以您认为合适的方式发布用户提供的其他元数据.
显然这是准系统,你应该在你认为合适的时候充实它.
由于您正在寻找一个库,我为您开发了一个完整的普通 Javascript 解决方案,如下所示:
\n\n/* as soon as all the elements of the page has been loaded */\r\ndocument.addEventListener(\'DOMContentLoaded\', function() {\r\n /* we reference the most used elements into variables, making it easier to use later */\r\n var dropzone = document.getElementById(\'dropzone\'),\r\n dialog = document.getElementById(\'dropzone-dialog\'),\r\n submit = document.getElementById(\'submit\'),\r\n progress = document.querySelector(\'progress\'),\r\n _current = null; // we keep track what file the user is changing its metadata\r\n\r\n /* when the user drags something over our div, we must prevent the browser\'s default\r\n behavior and we change a CSS class only for usability reasons */\r\n dropzone.addEventListener(\'dragover\', function(e) {\r\n e.preventDefault();\r\n dropzone.classList.add(\'dragging\');\r\n });\r\n\r\n /* when the user leaves our div, we must remove the CSS class we put before */\r\n dropzone.addEventListener(\'dragleave\', function(e) {\r\n e.preventDefault();\r\n dropzone.classList.remove(\'dragging\');\r\n });\r\n\r\n /* that\'s the heart of our code,\r\n when the user effectively drops one or more files into the div */\r\n dropzone.addEventListener(\'drop\', function(e) {\r\n /* again we must prevent the default browser\'s behavior,\r\n and we need to remove the CSS clas we put before */\r\n e.preventDefault();\r\n dropzone.classList.remove(\'dragging\');\r\n submit.classList.add(\'hidden\');\r\n\r\n /* we change our div\'s text */\r\n dropzone.textContent = \'File(s) dropped:\';\r\n\r\n /* we create an UL element, so we can loop through the files make a list */\r\n var ul = document.createElement(\'ul\');\r\n /* since the files property is not an array, but it is Array-like,\r\n we call the Array.prototype.forEach method passing the files to be the context */\r\n [].forEach.call(e.dataTransfer.files, function(file) {\r\n /* for each file the user has dropped, we create a LI and an anchor inside it */\r\n var li = document.createElement(\'li\');\r\n var a = document.createElement(\'a\');\r\n\r\n a.href = \'#\';\r\n a.textContent = file.name;\r\n a.title = \'Enter metadata\';\r\n \r\n /* we create a property __file into our anchor to reference the dropped file */\r\n a.__file = file;\r\n\r\n li.appendChild(a);\r\n ul.appendChild(li);\r\n });\r\n dropzone.appendChild(ul);\r\n });\r\n\r\n /* here, we add a \'click\' event handler to our div dropzone,\r\n so we can add the file\'s metadata behavior.\r\n the idea here is to make an event delegation, that is, when the user clicks anywhere\r\n inside the div, we see if the element that was clicked is one of the files */\r\n dropzone.addEventListener(\'click\', function(e) {\r\n var el = e.target;\r\n\r\n /* if the element clicked is an anchor, it\'s because it\'s one of the files */\r\n if (el.tagName === \'A\') {\r\n /* we prevent the browser\'s default behavior */\r\n e.preventDefault();\r\n /* we keep track that the current clicked file/anchor is the last clicked */\r\n _current = el;\r\n \r\n /* and then, we show our dialog, so the user can input the file\'s metadata */\r\n dialog.classList.remove(\'hidden\');\r\n /* we set focus on the first element of the dialog, only for usability reasons */\r\n dialog.querySelector(\'#name\').focus(); \r\n \r\n }\r\n });\r\n\r\n /* we add an event handler to the dialog\'s close button,\r\n so it can effectively close the dialog */\r\n dialog.querySelector(\'.close\').addEventListener(\'click\', clearDialog);\r\n \r\n /* this is the second heart of our code.\r\n we add an event handler to the dialog\'s save button,\r\n so it can handle the saving of metadata */\r\n dialog.querySelector(\'#save\').addEventListener(\'click\', function() {\r\n /* here you can add any validation you want, e.g: required fields, etc */\r\n \r\n /* if everything was cool, we set a new property __dataFile to our anchor\r\n so, we can save the metadata for future use (the form submission) */\r\n _current.__dataFile = {\r\n name: dialog.querySelector(\'#name\').value,\r\n category: dialog.querySelector(\'#category\').value\r\n /* put here any other metadata property you will save */\r\n };\r\n \r\n /* when the user saves the metadata, we add a CSS class only for usability reasons */\r\n _current.classList.add(\'finished\');\r\n\r\n /* then, we keep track if there is any file with no metadata saved yet */\r\n var btnsLeft = [].filter.call(dropzone.querySelectorAll(\'a\'), function(el, i) {\r\n return !(\'__dataFile\' in el);\r\n });\r\n\r\n /* if all the files\' metadatas have been saved, we show the submit button */\r\n if (btnsLeft.length === 0)\r\n submit.classList.remove(\'hidden\');\r\n\r\n /* and we clear/close our dialog */\r\n clearDialog();\r\n });\r\n\r\n function clearDialog() {\r\n /* when the dialog is closed, we set our current-clicked anchor to null */\r\n _current = null;\r\n\r\n /* we clean all the input fields of the dialog */\r\n [].forEach.call(dialog.querySelectorAll(\'input\'), function(el, i) {\r\n el.value = \'\';\r\n });\r\n\r\n /* and we effectively hide/close the dialog */\r\n dialog.classList.add(\'hidden\');\r\n }\r\n\r\n /* this is third heart of our code.\r\n we add a click event handler to our submit button,\r\n so we can effectively send our form to the server */\r\n submit.querySelector(\'button\').addEventListener(\'click\', function(e) {\r\n e.preventDefault(); // we must prevent the browser\'s default behavior\r\n\r\n /* we create a XMLHttpRequest to send our AJAX to the server */\r\n var xhr = new XMLHttpRequest();\r\n\r\n /* we add a \'load\' event handler, so we can keep track when the upload is finished */\r\n xhr.addEventListener(\'load\', finished);\r\n /* we do the same for progress and error, so we can inform our user what\'s going on */\r\n xhr.upload.addEventListener(\'progress\', progress);\r\n xhr.addEventListener(\'error\', error);\r\n \r\n /* now it\'s time to save all our data, so we create a FormData */\r\n var fd = new FormData();\r\n\r\n /* and for each anchor(file) inside our dropzone, we add new data to our FormData */\r\n [].forEach.call(dropzone.querySelectorAll(\'a\'), function(el, i) {\r\n /* here we loop through our __dataFile previously created property,\r\n so we can add the file\'s metadata parameter */\r\n for (var prop in el.__dataFile) {\r\n /* since we are sending multiple files/metadatas,\r\n it\'s important to name our field with [] */\r\n fd.append(prop + \'[]\', el.__dataFile[prop]);\r\n }\r\n \r\n /* and then, we append the file itself - again, with the [] at the end */\r\n fd.append(\'file[]\', e.__file);\r\n });\r\n\r\n /* now we open the xhr, and we effectively send the request to the server */\r\n xhr.open(\'POST\', \'/api/uploadfiles/\'); // change for your API URL\r\n xhr.send(fd);\r\n });\r\n \r\n /* this is the xhr\'s progress, so we can update our HTMLProgressElement\'s percent */\r\n function progress(e) {\r\n progress.value = progress.innerHTML = (e.loaded / e.total * 100 | 0);\r\n }\r\n\r\n /* this is the xhr\'s finished event handler,\r\n we show a friendly message and hide the submit button */\r\n function finished(e) {\r\n progress.value = progress.innerHTML = 100;\r\n dropzone.innerHTML = \'<h3>Files successfully uploaded</h3>\';\r\n submit.classList.add(\'hidden\');\r\n }\r\n \r\n /* this is the xhr\'s error event handler. If there is any known error, we show it */\r\n function error(e) {\r\n var xhr = e.target;\r\n submit.classList.add(\'hidden\');\r\n dropzone.innerHTML = \'<h3>Error while uploading</h3>\' +\r\n (xhr.status ? \'<span>\' + xhr.status + \' - \' + xhr.statusText + \'</span>\' : \'\');\r\n }\r\n});Run Code Online (Sandbox Code Playgroud)\r\n#dropzone {\r\n width: 450px;\r\n height: 165px;\r\n background-color: #CCC;\r\n font: 16px Verdana;\r\n padding: 10px;\r\n box-sizing: border-box;\r\n}\r\n#dropzone.dragging {\r\n background-color: #EEE;\r\n}\r\n#dropzone > h3 {\r\n text-align: center;\r\n}\r\n#dropzone a {\r\n text-decoration: none;\r\n}\r\n#dropzone a:hover {\r\n text-decoration: underline;\r\n}\r\n#dropzone a.finished:after {\r\n content: \' \xe2\x88\x9a\';\r\n color: green;\r\n font-weight: bold;\r\n text-decoration: none;\r\n}\r\n#dropzone ul {\r\n list-style: none;\r\n margin-left: 15px;\r\n padding: 0;\r\n}\r\n#dropzone li {\r\n margin-top: 8px;\r\n}\r\n#dropzone li:before {\r\n content: \'> \';\r\n}\r\n.hidden {\r\n display: none;\r\n}\r\n.dialog {\r\n background-color: #AAAAFF;\r\n font-family: Verdana;\r\n padding: 12px;\r\n border-radius: 10px;\r\n width: 300px;\r\n height: 150px;\r\n position: absolute;\r\n top: 20px;\r\n}\r\n.dialog h3 {\r\n text-align: center;\r\n}\r\n.dialog .close {\r\n text-decoration: none;\r\n float: right;\r\n}\r\n.dialog .close:after {\r\n content: \'\xe2\x9c\x96\';\r\n cursor: pointer;\r\n}\r\n.dialog > div {\r\n display: table;\r\n}\r\n.dialog > div > div {\r\n display: table-row;\r\n}\r\n.dialog > div > div > div,\r\n.dialog > div > div > label {\r\n display: table-cell;\r\n padding: 2px 0 2px 10px;\r\n}\r\n\r\n#submit {\r\n height: 160px;\r\n width: 450px;\r\n text-align: right;\r\n position: fixed;\r\n top: 10px;\r\n}\r\n#submit button {\r\n font-size: 20px;\r\n padding: 10px;\r\n background-color: orange;\r\n}\r\n\r\nprogress {\r\n width: 450px; \r\n}Run Code Online (Sandbox Code Playgroud)\r\n<div id="dropzone">\r\n <h3>Drop your files here</h3>\r\n</div>\r\n<div>\r\n <progress min="0" max="100" value="0"></progress>\r\n</div>\r\n<div id="submit" class="hidden">\r\n <button>Submit</button>\r\n</div>\r\n<div id="dropzone-dialog" class="dialog hidden">\r\n <a class="close"></a>\r\n <h3>File Metadata</h3>\r\n <div>\r\n <div>\r\n <label for="name">\r\n Name\r\n </label>\r\n <div>\r\n <input id="name">\r\n </div>\r\n </div>\r\n <div>\r\n <label for="category">\r\n Category\r\n </label>\r\n <div>\r\n <input id="category">\r\n </div>\r\n </div>\r\n <div>\r\n <div></div>\r\n <div style="text-align: right">\r\n <button id="save">Save</button>\r\n </div>\r\n </div>\r\n </div>\r\n</div>Run Code Online (Sandbox Code Playgroud)\r\n上面的代码已完整注释,但如果您需要任何帮助,请发表评论。
\nvvs*_*vvs -1
您是否尝试过 Dropzone.js http://www.dropzonejs.com/ \n他们似乎支持您正在寻找的功能。\n以下是相关页面的摘录。我自己没有尝试过,但纯粹基于值得尝试的文档来检查它是否满足您的要求。
\n\n\n\n如果禁用了 autoProcessQueue,则\xe2\x80\x99 需要
\n\n.processQueue()自己调用\n。如果您想要显示文件并让用户单击接受按钮来实际上传文件,这可能会很有用。
\n
| 归档时间: |
|
| 查看次数: |
5314 次 |
| 最近记录: |