简体   繁体   English

jQuery File Upload库:如何通过单击上传单个文件

[英]jQuery File Upload library: how to upload with a click for a SINGLE file

According to this section jQuery-File-Upload Basic plugin documentation , you can upload file upon a click of a button by using add . 根据本节jQuery-File-Upload Basic插件文档 ,单击add即可单击按钮来上传文件。 however, this causes an additional 'Upload' button to be added each time the user select a file, whereas my app only accepts a single upload. 但是,这会导致每次用户选择文件时都会添加一个额外的“上传”按钮,而我的应用仅接受一次上传。

see JS fiddle here 看到JS小提琴在这里

so how to achieve this: when user clicks Choose file the 2nd time, that file replaces the initial file and only one upload button is present at any moment. 因此如何实现:当用户第二次单击“选择文件”时,该文件将替换初始文件,并且在任何时候仅显示一个上传按钮。

Thanks! 谢谢!

It looks like a new button is getting added every time the 'Add' event fires with this line: 看起来每次“添加”事件在此行触发时,都会添加一个新按钮:

$('<button/>').text('Upload').appendTo(document.body)

You can prevent re-adding the button in multiple ways, here's a simple example: 您可以通过多种方式阻止重新添加按钮,这是一个简单的示例:

if ($("button:contains('Upload')").length == 0) {
    $('<button/>').text('Upload').appendTo(document.body)
}

You'll probably have to update the code to rebind the 'click' function with the new data being submitted. 您可能必须更新代码,才能将“ click”功能与正在提交的新数据重新绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM