gpt4 book ai didi

javascript - event.preventDefault 和 stopPropagation 在拖/放事件中未按预期工作

转载 作者:行者123 更新时间:2023-11-28 12:20:04 24 4
gpt4 key购买 nike

我正在 angular2 中实现文件拖/放,如下所示。

export class JobDescription implements AfterViewInit {

.....

noPropagation:any=(e)=>{
e.stopPropagation();
if (e.preventDefault) {
return e.preventDefault();
} else {
return e.returnValue = false;
}
};
constructor(public router: Router,public route:ActivatedRoute, public http: Http,public appState: AppState,private $uibModal: NgbModal,private renderer:Renderer) {
this.main=appState._state;
.....
}
ngAfterViewInit() {
this.dragViewInit();
}

........

FileDragHover(e){
e = e || event;
this.noPropagation(e);
jQuery(".upload-cta").addClass("upload_cta_active");
};
FileSelectHandler(e){
e = e || event;
this.noPropagation(e);
this.FileDragHover(e);
this.droppedFiles = e.target.files || e.dataTransfer.files;
};


dragViewInit(){
this.appState.set("dragViewInit",function () {
console.log("Init called");
setTimeout(()=>{
var fileselect = document.getElementById("fileselect"),
filedrag = document.getElementById("dropzone");
fileselect.addEventListener("change", this.FileSelectHandler, false);
var xhr = new XMLHttpRequest();
if (xhr.upload) {
filedrag.addEventListener("dragover", this.FileDragHover, false);
filedrag.addEventListener("dragleave", this.FileDragHover, false);
filedrag.addEventListener("drop", this.FileSelectHandler, false);
}
console.log("Init is initilised");
},500)
})
}
}

在我的服务appState中如下:

this._state['open'] = function(tmplt, modalSize) {
console.log("tmplt",tmplt);
this['modalInstance'] = ngbModal.open(tmplt, {size: modalSize});
};

this._state['uploadCV'] = function(content){
this.open(content, 'lg');
this.dragViewInit();
};

我的html如下:

<template ngbModalContainer></template>
<template #uploadCV let-c="close" let-d="dismiss">
<div class="modal-getintouch">
<div class="modal-header">
<h4 class="modal-title text-uppercase">Upload your CV</h4>
<button type="button" class="close" (click)="c('close')">
<i class="bi_interface-cross"></i>
</button>
</div>
<div class="modal-body">
<section id="dropzone" class="upload-cta pad-t-1 dropzone">
<div class="">
<div class="upload-cv-icon col-lg-offset-4"></div>
<p class="center-text pad-b-3">
Drag and drop your resume here<br />
<span class="browse-files">You can also <a href="javascript:void(0)" (click)="clickInp(myFilInp)">browse and choose files</a></span>
</p>
<p>{{fileDesp}}</p>
<input class="box__file" type="file" #myFilInp name="files[]" id="fileselect" style="display: none" (change)="showFiles()" />

</div>
</section>
<div class="row">
<div class="col-12">
<div *ngIf="filProgress" class="progress">
<div class="progress-bar progress-bar-striped active" role="progressbar"
[attr.aria-valuenow]="filProgress" aria-valuemin="0" aria-valuemax="100" [ngStyle]="{width: filProgress+'%'}">
40%
</div>
</div>
</div>
</div>
<div class="row">
<button type="button" [ngClass]="{'btn':true, 'btn-success':true,'btn-default':true, 'pull-right':true}" (click)="upload()">
<i class="fa fa-upload"></i> Upload
</button>
</div>
</div>
</div>
</template>

但是当我将图像文件拖放到 dropzone 时,它会在浏览器中打开该文件。

最佳答案

下面的实现没有jquery,使用 angular2 和 typescript

HTML

<div (dragover)="allowDrop($event)" (drop)="onFileDrop($event)"
[class.drag-over]='allowFileDrop'>
<div class="file-upload-button">
<label>
Upload Resume
<input class="hide-file" type="file" (change)="onFileChange($event)" placeholder="Upload file..." accept=".pdf,.doc,.docx"/>
</label>
</div>
</div>

typescript

allowDrop(event): void {
event.preventDefault();
event.dataTransfer.setData("Drop Here", event.target.id);
this.allowFileDrop = true; // highlight drop zone on drag over using css class
}

onFileDrop(event) {
event.preventDefault();
this.allowFileDrop = false;
let fileList: FileList = event.dataTransfer.files;
this.uploadResume(fileList);
}

onFileChange(event) {
let fileList: FileList = event.target.files;
this.uploadResume(fileList);
}

private uploadResume(fileList: FileList){
if(fileList.length > 0){
let file: File = fileList[0];
console.log(file.name);
}
}

关于javascript - event.preventDefault 和 stopPropagation 在拖/放事件中未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40338421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com