简体   繁体   English

Angular 2 File Upload示例

[英]Angular 2 File Upload sample

Please could you share a simple file upload sample that conveys the basics of such functionality when using Angular 2? 请您分享一个简单的文件上传示例,以传达使用Angular 2时此类功能的基础吗?

The research I did (am doing) keeps bubbling up older versions of Angular. 我所做的研究(正在做)一直在冒泡Angular的旧版本。 At this point all I'm trying to do is load a small image file into a property, or local storage, and have it displayed on the webpage. 此时,我要做的就是将一个小的图像文件加载到属性或本地存储中,并将其显示在网页上。

I imagine it implies the use of HttpClient however as I'm new to Angular 2 it's challenging to drum up the beginnings of how this works without leaning on a simple sample. 我想象这意味着使用HttpClient,但是由于我是Angular 2的新手,因此在不依靠简单示例的情况下鼓起它的工作原理是一项挑战。

I'm trying this and it's not having the effect I was expecting. 我正在尝试此方法,但没有达到我期望的效果。 It feels like I'm missing an import. 感觉好像我缺少导入。 This portion is only trying to get the name of the file out, not the image rendered. 此部分仅尝试获取文件名,而不显示图像。 It appears using [(ngModel)] is not an option, and I must resort to change event to get the selected file. 它不是使用[[ngModel)]出现的,我必须诉诸更改事件来获取所选文件。

Component 零件

@Component({
    selector:'home-page'
    ,templateUrl:'./home.page.html'
})
export class HomePageComponent{
    CurrentFile:File;
    public FileChangeEvent(fileInput:any){
        this.CurrentFile = fileInput.target.files[0];
    }
}

html html

<div>
    <input type="file" (change)="FileChangeEvent($event)">
    <div *ngIf="CurrentFile">
        <p>{{CurrentFile.name}}</p>
    </div>
</div>

Everything you need there to upload files in Angular 2: 在Angular 2中上传文件所需的一切:

https://github.com/valor-software/ng2-file-upload https://github.com/valor-software/ng2-file-upload

After a rough go of it I got what I think can be considered bare basics. 经过一番粗略的研究后,我得到了我认为可以视为基本知识的内容。 There is no attempt to handle things such as when no image is selected, aside from when the page loads and file is unknown. 除了页面加载的时间和文件未知之外,没有尝试处理诸如未选择图像的情况。

Component 零件

@Component({
    selector:'home-page'
    ,templateUrl:'./home.page.html'
})
export class HomePageComponent{
    CurrentFile:File;
    ImageSource:string;

    public FileChangeEvent(fileInput:any){
        this.CurrentFile = fileInput.target.files[0];

        let reader = new FileReader();
        reader.onload = () => {
                this.ImageSource = reader.result;
            };
        reader.readAsDataURL(this.CurrentFile);
    }
}

HTML 的HTML

<div>
    <input type="file" (change)="FileChangeEvent($event)">
    <div *ngIf="CurrentFile">
        <img [src]="ImageSource">
    </div>
</div>

You can use this answer to upload the file to your server: 您可以使用以下答案将文件上传到服务器:

https://stackoverflow.com/a/40216616/5413117 https://stackoverflow.com/a/40216616/5413117

You can then for example upload it to azure and show the image through the azure storage link. 然后,您可以例如将其上传到azure,并通过azure存储链接显示图像。

If you want to get rid of using the file changed event, although it works, you can write a control value accessor for file inputs, explained here: 如果您想摆脱使用文件更改事件的麻烦,尽管它可以工作,但是您可以为文件输入编写一个控制值访问器,在此进行解释:

https://stackoverflow.com/a/41938495/5413117 https://stackoverflow.com/a/41938495/5413117

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM