简体   繁体   English

HTML 5拖放到输入文件

[英]Html 5 drag and drop to input file

I have no idea if this is possible, I can't seem to find anything. 我不知道这是否可行,我似乎什么也找不到。

What I want is to offer the user the option of either "drag drop" an image or use the normal html file input box. 我想要为用户提供“拖放”图像或使用常规html文件输入框的选项。

I don't the image to be uploaded until the form is saved. 在保存表单之前,我不会上传图片。 So I guess the drag and drop area would just update the field? 所以我猜拖放区域会更新字段吗?

How would I go about doing that? 我将如何去做?

Thanks in advance. 提前致谢。

I came here looking for the same answer. 我来这里寻找相同的答案。 Ended up do the following, which looks to fit your need. 最终,请执行以下操作,这将满足您的需求。

Make a div for your image drop box, then add your input type="file" inside the div. 为您的图像放置框创建一个div,然后在div内添加输入type =“ file”。 Set height and width as desired. 根据需要设置高度和宽度。 Set opacity to 0. Now your input fills the div, can't be seen, but still functions. 将opacity设置为0。现在您的输入将填充div,虽然看不到,但仍能正常工作。

<div class="divClass">
  <input class="inputClass" name="inputName" type="file">
</div>

Styles below 下面的样式

<Style>
.divClass{
  position: relative;
  // anything else
}
.inputClass{
  opacity: 0;
  position: absolute;
  top:0;
  left: 0;
  height: x // as desired
  width: x // as desired
}
</style>

=== Edit === ===编辑===

Ignore my styling up there, you really just need to use a form with an input type=file. 忽略我的样式,您实际上只需要使用输入类型为= file的表单。 Style/layout as needed. 根据需要设置样式/布局。

As pointed out, by Shazoo, drag and drop of image does not work in IE. 如前所述,Shazoo表示,图像的拖放在IE中不起作用。 Works with Firefox and Chrome. 适用于Firefox和Chrome。 Unsure of Opera and Safari. 不确定Opera和Safari。 You cannot use JavaScript on the input type=file for security reasons -- it is read only as Jasny said, so you have to rely on what the browser vendor allows. 出于安全原因,您不能在输入type = file上使用JavaScript-就像Jasny所说的那样,它是只读的,因此您必须依靠浏览器供应商的允许。

With that in mind, you can just give the IE user the option of clicking the field to select and upload a file. 考虑到这一点,您可以为IE用户提供单击字段以选择和上传文件的选项。 The default input type="file" in IE offers this ability, so as long as you leave the input visible, they should see it. IE中的默认输入type =“ file”提供了此功能,因此只要您保持输入可见,他们就应该看到它。 (Default Edge input type=file) (默认边缘输入类型=文件)

边缘示例输入类型=文件

Filter for IE example (based on https://stackoverflow.com/a/31757969/3136874 ) 过滤IE示例(基于https://stackoverflow.com/a/31757969/3136874

function stopIeDefault(){
  window.addEventListener("dragover",function(e){
    e = e || event;
    e.preventDefault();
  },false);
    window.addEventListener("drop",function(e){
    e = e || event;
    e.preventDefault();
  },false);
}
if (/MSIE 10/i.test(navigator.userAgent)) {
  // This is internet explorer 10
  stopIeDefault()
}

if (/MSIE 9/i.test(navigator.userAgent) || /rv:11.0/i.test(navigator.userAgent)) {
  // This is internet explorer 9 or 11
  stopIeDefault()
}

if (/Edge\/\d./i.test(navigator.userAgent)){
  // This is Microsoft Edge
  stopIeDefault()
}   

This tests for IE and edge. 这会测试IE和Edge。 If it detects a version then it will use stopIeDefault to preventDefault for drag and drop. 如果检测到版本,则它将使用stopIeDefault来防止拖放。 This keeps IE from loading the image if someone drags and drops it on the form. 如果有人将其拖放到窗体上,这将阻止IE加载图像。 (Edge appears to prevent this by default, so you may not need to call stopIeDefault as I have.) Additionally, you could apply a different style or add blocks of code or what not to deal with IE. (默认情况下,Edge似乎阻止了此操作,因此您可能不需要像我一样调用stopIeDefault。)此外,您可以应用其他样式或添加代码块或不处理IE的代码。 According to w3schools, IE browser share is 5.2-6.2% as of August 2016: http://www.w3schools.com/browsers/default.asp , if that helps you decide what to do. 根据w3schools的数据,截至2016年8月,IE浏览器的份额为5.2-6.2%: http : //www.w3schools.com/browsers/default.asp ,如果这可以帮助您决定要做什么。

If anyone can confirm if drag and drop works on safari or opera, please do. 如果有人可以确认拖放是否适用于野生动物园或歌剧,请这样做。 === Edit === ===编辑===

No it can't be done. 不,它无法完成。 Dropped files can only be uploaded through AJAX. 删除的文件只能通过AJAX上传。

The only way to upload a file in a normal HTML form is through <input type="file"> and file inputs are read-only . 以普通HTML格式上载文件的唯一方法是通过<input type="file">并且文件输入为只读

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

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