从sof上找到一个example:https://stackoverflow.com/questions/46206643/asp-net-core-2-0-and-angular-4-3-file-upload-with-progress,不但上传文件,而且支持多文件:

模板代码:

<input #file type="file" multiple (change)="upload(file.files)" />
<span *ngIf="uploadProgress > 0 && uploadProgress < 100">
{{uploadProgress}}%
</span>

组件代码:

import { Component } from '@angular/core';
import { HttpClient, HttpRequest, HttpEventType, HttpResponse } from '@angular/common/http' @Component({
selector: 'files',
templateUrl: './files.component.html',
})
export class FilesComponent {
public uploadProgress: number; constructor(private http: HttpClient) { } upload(files) {
if (files.length === 0)
return; const formData = new FormData(); for (let file of files)
formData.append(file.name, file); const req = new HttpRequest('POST', `api/files`, formData, {
reportProgress: true,
}); this.http.request(req).subscribe(event => {
if (event.type === HttpEventType.UploadProgress)
this.uploadProgress = Math.round(100 * event.loaded / event.total);
else if (event instanceof HttpResponse)
console.log('Files uploaded!');
});
}
}

单文件上传,改改就行。

另外那个FormData接口的使用需要IE9兼容填充库:https://angular.cn/guide/browser-support#建议的填充库

Angular HttpClient upload file with FormData的更多相关文章

  1. httpclient upload file

    用httpclient upload上传文件时,代码如下: HttpPost httpPost = new HttpPost(uploadImg); httpPost.addHeader(" ...

  2. post upload file & application/x-www-form-urlencoded & multipart/form-data

    post upload file application/x-www-form-urlencoded & multipart/form-data https://stackoverflow.c ...

  3. 页面无刷新Upload File

    页面无刷新Upload File. 利用jquery.form.js的ajaxForm提交文件. 具体参考以下代码: 前台html <%@ Page Language="C#" ...

  4. jQuery文件上传插件jQuery Upload File 有上传进度条

    jQuery文件上传插件jQuery Upload File 有上传进度条 jQuery文件上传插件jQuery Upload File,插件使用简单,支持单文件和多文件上传,支持文件拖拽上传,有进度 ...

  5. Upload file

    <h3>Upload File</h3> <form action="@Url.Action("Upload","UploadCo ...

  6. Express web框架 upload file

    哈哈,敢开源,还是要有两把刷子的啊 今天,看看node.js 的web框架 Express的实际应用 //demo1 upload file <html><head><t ...

  7. fetch API & upload file

    fetch API & upload file https://github.com/github/fetch/issues/89 https://stackoverflow.com/ques ...

  8. 通过iframe 实现upload file无刷新

    <html>    <head> </head> <body> <form encType="multipart/form-data&q ...

  9. apache php upload file

    /********************************************************************************* * apache php uplo ...

随机推荐

  1. A*寻路算法入门(三)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  2. (NO.00003)iOS游戏简单的机器人投射游戏成形记(十六)

    回到MainScene.m中添加selectRobot方法: -(void)selectRobot:(Robot *)robot{ LevelRestrict *lr = [LevelRestrict ...

  3. javascript语法之String对象

    学习String类就是学习它的一些方法,主要用到方法全部罗列出来.如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transition ...

  4. Credit Summaries & Importing External Credit Exposure

    In this Document   Goal   Solution APPLIES TO: Oracle Order Management - Version 11.5.10.2 to 12.1.3 ...

  5. Git版本控制:Git查阅、撤销文件修改和撤销文件追踪

    http://blog.csdn.net/pipisorry/article/details/47867097 查看文件的修改历史 git log --pretty=oneline 文件名 # 显示修 ...

  6. (二十七)QQ好友列表的实现

    QQ好友列表通过plist读取,plist的结构为一组字典,每个字典内有本组的信息和另外一组字典代表好友. 要读取plist,选择合适的数据结构,例如NSArray,然后调用initWithConte ...

  7. shell的date命令:使用方法,以及小时、分钟的计算

    shell命令格式严格,不像python那样命令行中可以添加空格.如等号两边无空格.有多余空格错误,日期date命令就是最明显的例子. 命令格式: date [-u] [-d datestr] [-s ...

  8. ARM-linux汇编常用语法

    ARM linux常用汇编语法 ============================= 汇编语言每行的语法: lable: instruction ; comment 段操作: .section ...

  9. Leetcode_49_Anagrams

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42744709 Given an array of stri ...

  10. DigitalClock的替代者TextClock

    DigitalClock在API 17(Android4.2)中就被官方定义为过时的方法,很奇怪为什么现在很多的网上的帖子都介绍DigitalClock方法的使用(已经过时),如果你想让自己的应用在高 ...