【问题标题】:How to upload image using dropzone in angular 4 on button click如何在按钮单击时使用 Angular 4 中的 dropzone 上传图像
【发布时间】:2018-04-06 06:18:27
【问题描述】:

我正在使用 dropzone 在按钮单击事件上从 angular 4 应用程序将图像上传到 webApi。 mydrpzone.processQueue() 不工作。在上传按钮点击我得到这个错误 this.drpzone.processQueue is not a function.

这是我的代码

app.component.ts

import { Component, ViewChild } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { DropzoneModule, DropzoneComponent, DropzoneDirective, 
DropzoneConfigInterface } from 'ngx-dropzone-wrapper';


@Component({
selector: 'my-app',
templateUrl: `./app.component.html`,
styleUrls: ['./app.component.css']
})
export class AppComponent {

UsreName: string = "Midhun";
@ViewChild('drpzone') drpzone: DropzoneConfigInterface;



myFiles: string[] = [];
sMsg: string = '';

getFileDetails(e: any) {

    //console.log (e.target.files);
    for (var i = 0; i < e.target.files.length; i++) {
        this.myFiles.push(e.target.files[i]);
    }


}

onSending(data: any): void {
    // data [ File , xhr, formData]
    const file = data[0];
    const formData = data[2];
    formData.append('Name', "Midhun");
    console.log("enetered");
}

uploadFiles() {
    //this.drpzone.processQueue();
    this.drpzone.processQueue();
    console.log("uploading...");
}

onSendingmultiple() {

}

onError() {

}
onSuccess() {

}


//public type: string = 'component';
public type: string = 'directive';

public config: DropzoneConfigInterface = {
    url: 'http://localhost:60945/api/fileupload/',
    //url: 'http://localhost:60945/user/PostUserImage',
    //url:'https://httpbin.org/post',
    maxFiles: 5,
    clickable: true,
    acceptedFiles: 'image/*',
    createImageThumbnails: true,
    autoProcessQueue: false,
    addRemoveLinks: true,

};

constructor() { }


}

app.component.html

<div class="text-center well">
            <dropzone [config]="config" #drpzone
                      [message]="'Click or drag images here to upload'"
                      (error)="onError($event)"
                      (sending)="onSending($event)"
                      (sendingmultiple)="onSendingmultiple($event)"
                      (success)="onSuccess($event)">
            </dropzone>
        </div>
        <br />
        <button (click)="uploadFiles()">Upload</button>

如果有人知道如何解决它,请提供帮助。

【问题讨论】:

    标签: angular dropzone.js


    【解决方案1】:

    刚用谷歌搜索了一下,发现是这样的: 该指令本身不是 Dropzone 实例,因此对于 4.x this.dropzone.dropzone.processQueue() 或对于 5.x this.dropzone.dropzone().processQueue()。

    到这里:https://github.com/zefoy/ngx-dropzone-wrapper/issues/60

    顺便说一句,我不确定,但这似乎不正确 @ViewChild('drpzone') drpzone: DropzoneConfigInterface;

    不应该 @ViewChild('drpzone') drpzone: DropzoneDirective; ?

    【讨论】:

    • 感谢您的回复。但它不工作..我之前有那个代码的场景。它给了我这个错误 ERROR TypeError: Cannot read property 'processQueue' of undefined
    • 你找到什么了吗?
    猜你喜欢
    • 2013-04-19
    • 2017-11-23
    • 1970-01-01
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多