我试图创建一个控件,在该控件中,您可以 Select 要在表单中提交的文件,并将一个文件放入其中以执行相同的操作.我有这样的东西,如果是图像,它也会显示文件的预览:
<div class="preview-image-container">
<input type="file" name="File" id="File" accept="image/*"
class="image-url form-control" style="display:none;" />
<div class="preview-image-dummy"></div><img class="preview-image-url" />
<div class="preview-image-instruction">
<div class="preview-image-btn-browse btn btn-primary">Select file</div>
<p>or drop it here.</p>
</div>
</div>
用户将文件放入preview-image-container
.文件不是用AJAX提交的,用户需要提交包含更多数据的表单.
For security reasons, we aren't allowed to change the value of the input file with JavaScript. However, I know that the default input file support droppable and there's a bunch of websites that let us select files by dropping them in the form so my guess is that there's a way to do it.
As you can see from the MCVE, I am only interested in using jQuery or pure JavaScript without additional libraries.
虽然可以使用dropzone.js,但它不符合我的要求,需要大量的时间来定制它的外观.此外,dropzone.js具有某些在我的ASP中无法满足的配置要求.NET应用程序.
There's a similar question but does not have a proper answer:
How to set file object into input file in the form in HTML?
也不类似于drag drop images input file and preview before upload,因为我已经实现了拖放和预览操作.My question is specific to the problem of having an empty file input when dropping the file in a parent container.