[英]Drag and drop events for image file upload not working in Aurelia
I need help with my image upload function for drag and drop. 我需要图像上传功能的拖放帮助。
I get a nice box saying I can drag and drop images but my console's is saying: 我收到一个不错的框,说我可以拖放图像,但是控制台的意思是:
Uncaught ReferenceError: drag is not defined Uncaught ReferenceError: drop is not defined Uncaught ReferenceError: dragNdrop is not defined 未捕获的ReferenceError:未定义拖动未 捕获的ReferenceError:未定义拖放未 捕获的ReferenceError:未定义dragNdrop
When I'm clicking and drag images. 当我单击并拖动图像时。
What am I missing? 我想念什么?
Here's a sample of my code: 这是我的代码示例:
html HTML
<div class="form-group">
<label for="imageInputFile">Ladda upp bild</label>
<input ref="fileInput" type="file" id="imageInputFile"
click.delegate="previewImage()">
</div>
<strong>OR</strong>
<span class="dragBox">
Drag and Drop image here
<input type="file" onChange="dragNdrop(event)" ondragover="drag()" ondrop="drop()" id="imageInputFile" />
</span>
<div id="preview"></div>
js JS
dragNdrop(event) {
let fileName = URL.createObjectURL(event.target.files[0]);
let preview = document.getElementById("preview");
let previewImg = document.createElement("img");
previewImg.setAttribute("src", fileName);
preview.innerHTML = "";
preview.appendChild(previewImg);
}
drag() {
document.getElementById('imageInputFile').parentNode.className = 'draging dragBox';
}
drop() {
document.getElementById('imageInputFile').parentNode.className = 'dragBox';
}
EDIT HTML 编辑HTML
<span class="dragBox">
Click here or drag images here
<input ref="fileInput" type="file" change.trigger="dragNdrop($event)"
dragover.delegate="drag()" drop.delegate="drop()"
id="imageInputFile" />
</span>
<div id="preview"></div>
my regular button for image upload works to add a new image, but this doesn't seem to do the trick. 我的常规图像上传按钮可以添加新图像,但这似乎不可行。
When you use HTML attributes like onChange
, ondragover
etc without delegate
, it will look for a drag
function in the global scope. 当您使用不带
delegate
HTML属性(如onChange
, ondragover
等)时,它将在全局范围内查找drag
功能。 And not in the instance of the aurelia's viewmodel bound to this view. 而不是aurelia的视图模型绑定到该视图。 You need to use
delegate
to let aurelia know that the instance of your class must be used. 您需要使用
delegate
来让aurelia知道必须使用您的类的实例。
You have two elements with the same id
in your HTML. 您的HTML中有两个具有相同
id
元素。 getElementById('imageInputFile')
is probably getting the wrong element. getElementById('imageInputFile')
可能获取了错误的元素。 Not sure why you have an input
with a type file
for previewing image, but you can change it to a button. 不确定为什么要
input
带有用于预览图像的类型file
的input
,但是可以将其更改为按钮。
You need to add the upload logic to drop
as well. 您还需要添加上传逻辑来
drop
。 I suggest moving the drag and drop to the span
since it covers the area where you want to drop. 我建议将拖放移动到
span
因为它覆盖了您要放置的区域。 Your input won't trigger any change
when drop
the file. drop
文件时,您的输入不会触发任何change
。
You don't need to add classes and set src
attributes manually using the DOM API. 您无需使用DOM API手动添加类并设置
src
属性。 For example, you can add a isDragging
property to your class. 例如,您可以将
isDragging
属性添加到您的类。 Based on this boolean property, set the draging
class to the span
. 基于此布尔属性,将
draging
类设置为span
。 You can add a <img src.bind="imageSrc" />
inside <div id="preview"></div>
. 您可以在
<div id="preview"></div>
内添加<img src.bind="imageSrc" />
<div id="preview"></div>
。 And set the imageSrc
property inside dragNdrop
function. 并在
dragNdrop
函数中设置imageSrc
属性。
Class: 类:
dragNdrop(event) {
const self = this;
let fileName = URL.createObjectURL(event.target.files[0]);
self.imageSrc = fileName; // that's it
}
drag() {
const self = this;
self.isDragging = true;
}
drop(event) {
const self = this;
self.isDragging = false;
self.dragNdrop(event);
}
HTML: HTML:
<button type="button" click.delegate="previewImage()">Preview</button>
<span class="dragBox ${isDragging ? 'draging' : ''}"
dragover.delegate="drag()"
drop.delegate="drop($event)">
Click here or drag images here
<input id="imageInputFile" type="file" change.trigger="dragNdrop($event)" />
</span>
<div id="preview">
<img show.bind="imageSrc" src.bind="imageSrc" />
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.