【发布时间】:2019-06-17 17:32:22
【问题描述】:
这里是 stackblitz 链接:- https://stackblitz.com/edit/angular6-ledera?file=app%2Fapp.component.ts
我正在尝试直接从桌面等拖放图像并放在 dropzone div 上。
1) 获取图像预览 2) 获取文件对象。
.html
<div *ngIf="!imageDrop" class="col-12 rmpm dropzone" appDrag>
<div class="text-wrapper">
<div class="centered">Drop your file here!</div>
</div>
</div>
<!--droped image preview-->
<img *ngIf="imageDrop" [src]="imageDrop" width="100px" height="100px">
dragDrop.directive.ts
@HostBinding("style.background") private background = "#eee";
constructor() {}
@HostListener("dragover", ["$event"]) public onDragOver(evt) {
evt.preventDefault();
evt.stopPropagation();
this.background = "#999";
console.log( '4444:::' + JSON.stringify(evt.target.files));
}
@HostListener("dragleave", ["$event"]) public onDragLeave(evt) {
evt.preventDefault();
evt.stopPropagation();
this.background = "#eee";
console.log( '222:::' + JSON.stringify(evt.target.files));
}
@HostListener("drop", ["$event"]) public onDrop(evt) {
evt.preventDefault();
evt.stopPropagation();
let files = evt.dataTransfer.files;
if (files.length > 0) {
this.background = "#eee";
console.log( '1111:::' + JSON.stringify(files));
console.log( '33333:::' + JSON.stringify(evt.target.files));
}
}
【问题讨论】:
-
您觉得我的回答有帮助吗?
标签: javascript angular html drag-and-drop image-upload