简体   繁体   English

在父组件中上传Angular 2触发文件

[英]Angular 2 trigger file upload in parent component

i want to trigger file upload in router parent component. 我想在路由器父组件中触发文件上传。 It should call two functions that are present in parent, openFileSelect and uploadSelectedFile to control the <input type="file"> element. 它应该调用父级中存在的两个函数openFileSelectuploadSelectedFile来控制<input type="file">元素。

My router looks like this: 我的路由器如下所示:

{ 
    path: '', 
    component: HomeComponent,
    canActivate: [AuthService],
    children: [
        {
            path: '',
            redirectTo: '/dashboard',
            pathMatch: 'full'
        },
        {
            path: 'dashboard',
            component: DashboardComponent
        },
        {
            path: 'upload',
            component: UploadComponent
        }
    ]
}

The file input is in HomeComponent.html template and router-outlet where the child component is displayed: 输入的文件位于HomeComponent.html模板和router-outlet中,其中显示子组件:

<div class="container">
    <input type="file" (change)="fileChangeEvent($event)" #fileUpload>
    <router-outlet></router-outlet>
</div>

Then in HomeComponent.ts i have: 然后在HomeComponent.ts中,我有:

import { Component } from "@angular/core";

@Component({
    templateUrl: "home.component.html",
})
export class HomeComponent {

    filesToUpload: Array<File>;

    constructor() {
        this.filesToUpload = [];
    }

    openFileSelect() {
        // TODO - How to programmatically trigger click event on <input type="file"> ?
    }

    uploadSelectedFile() {
        this.makeFileRequest("http://localhost:3000/upload", [], this.filesToUpload).then((result) => {
             console.log(result);
        }, (error) => {
             console.error(error);
        });
    }

    fileChangeEvent(fileInput: any){
        this.filesToUpload = <Array<File>> fileInput.target.files;
    }

    makeFileRequest(url: string, params: Array<string>, files: Array<File>) {
        ... XHR request ...
    }
}

At the end i want to trigger the same openFileSelect method from HomeComponent inside UploadComponent 最后,我想从UploadComponent内的HomeComponent触发相同的openFileSelect方法

import { Component } from "@angular/core";

@Component({
    templateUrl: "upload.component.html",
})
export class UploadComponent {

    constructor() { }

    openFileSelectInHomeComponent() {
        // TODO - how to call HomeComponent.openFileSelect() ?
    }
}

So i have two questions: 所以我有两个问题:

  • How to programmatically trigger click event on ? 如何以编程方式触发on的click事件?

  • And how to call function from child component to router parent component? 以及如何从子组件到路由器父组件调用函数?

There is a similar answer to your question here: jQuery : simulating a click on a <input type="file" /> doesn't work in Firefox? 您的问题在这里有类似的答案: jQuery:模拟对<input type =“ file” />的单击在Firefox中不起作用?

Maybe it will give you an idea. 也许会给你一个主意。 Or you can use a UI control like primeNg's FileUpload control. 或者,您可以使用诸如primeNg的FileUpload控件之类的UI控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有ng2-file-upload的Angular2 +文件上传-子组件不断调用父组件函数 - Angular2+ File Upload with ng2-file-upload - Child component keeps calling Parent Component functions 父组件中的触发事件来自路由Angular中子组件的加载 - Trigger event in Parent component from child component load in routing Angular 如何从 angular 中的父组件触发子组件的 function? - How to trigger a function of child component from parent in angular? 在Angular 2中使用输入/输出事件触发父组件中的方法 - Using input/output events to trigger methods in a parent component in Angular 2 如何从 angular 中的父组件触发 ngbDropdown 方法? - How to trigger ngbDropdown methods from parent component in angular? 父组件的触发状态 - trigger state of parent component 如何使用模板表单从Angular 2的父组件中触发子组件验证器 - How to trigger child component validators from parent component in Angular 2 using template forms 角度上传组件,设置最小和最大上传文件大小 - angular upload component, set min and max uploaded file size 使用Primeng上传组件上传文件 - Upload file with Primeng upload component 如何通过单击子组件触发父组件DidUpdate - How to trigger parent componentDidUpdate by click in child component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM