我在Angular 6上使用了“ ngx-file-drop”。
<file-drop headertext="Drop files here" (onFileDrop)="dropped($event)"
(onFileOver)="fileOver($event)" (onFileLeave)="fileLeave($event)" multiple>
<span>optional content (don't set headertext then)</span>
</file-drop>
组件文件是
public dropped(event: UploadEvent) {
this.files = event.files;
for (const droppedFile of event.files) {
// Is it a file?
if (droppedFile.fileEntry.isFile) {
const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
fileEntry.file((file: File) => {
// Here you can access the real file
// console.log("dropfile_file"+ droppedFile.relativePath, file);
this.drop_files.push(file);
console.log(this.drop_files);
});
} else {
// It was a directory (empty directories are added, otherwise only files)
const fileEntry = droppedFile.fileEntry as FileSystemDirectoryEntry;
console.log("file_entry"+ droppedFile.relativePath, fileEntry);
}
}
}
public fileOver(event){
console.log("file_over"+event);
}
public fileLeave(event){
console.log("file_leave"+event);
}
我不知道如何使用ngx-file-drop验证文件。
ngx-file-drop中是否有任何方法可以验证文件?请帮忙。
谢谢
答案 0 :(得分:0)
我使它像这样工作:
public dropped(event: UploadEvent) {
if (event.files.length > 1) {
alert("impossible de rajouter plus d'un document à la fois");
} else {
this.verifierEnvoyerDocument(event.files[0]);
}
}
verifierEnvoyerDocument(droppedFile: UploadFile) {
this.file = droppedFile;
// Is it a file and is it allowed?
if (droppedFile.fileEntry.isFile && this.isFileAllowed(droppedFile.fileEntry.name)) {
const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
fileEntry.file((file: File) => {
console.log('isFile :', file.name);
console.log(droppedFile.relativePath, file);
this.envoyerDocument(droppedFile.relativePath, file);
});
} else {
alert("Seul les fichiers au format '.doc', '.docx', '.ppt', '.pptx', '.pdf' sont accepté.");
}
}
isFileAllowed(fileName: string) {
let isFileAllowed = false;
const allowedFiles = ['.doc', '.docx', '.ppt', '.pptx', '.pdf'];
const regex = /(?:\.([^.]+))?$/;
const extension = regex.exec(fileName);
if (isDevMode()) {
console.log('extension du fichier : ', extension);
}
if (undefined !== extension && null !== extension) {
for (const ext of allowedFiles) {
if (ext === extension[0]) {
isFileAllowed = true;
}
}
}
return isFileAllowed;
}
希望这对某人有帮助。
答案 1 :(得分:0)
import { UploadEvent, UploadFile, FileSystemFileEntry, FileSystemDirectoryEntry } from 'ngx-file-drop';
public dropped(event: UploadEvent) {
let droppedFile = event.files[0];
if (droppedFile.fileEntry.isFile) {
const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
fileEntry.file((file: File) => {
this.file = file;
});
}
}
在多个循环event.files中