Angular HttpClient upload file with FormData
从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的更多相关文章
- httpclient upload file
用httpclient upload上传文件时,代码如下: HttpPost httpPost = new HttpPost(uploadImg); httpPost.addHeader(" ...
- 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 ...
- 页面无刷新Upload File
页面无刷新Upload File. 利用jquery.form.js的ajaxForm提交文件. 具体参考以下代码: 前台html <%@ Page Language="C#" ...
- jQuery文件上传插件jQuery Upload File 有上传进度条
jQuery文件上传插件jQuery Upload File 有上传进度条 jQuery文件上传插件jQuery Upload File,插件使用简单,支持单文件和多文件上传,支持文件拖拽上传,有进度 ...
- Upload file
<h3>Upload File</h3> <form action="@Url.Action("Upload","UploadCo ...
- Express web框架 upload file
哈哈,敢开源,还是要有两把刷子的啊 今天,看看node.js 的web框架 Express的实际应用 //demo1 upload file <html><head><t ...
- fetch API & upload file
fetch API & upload file https://github.com/github/fetch/issues/89 https://stackoverflow.com/ques ...
- 通过iframe 实现upload file无刷新
<html> <head> </head> <body> <form encType="multipart/form-data&q ...
- apache php upload file
/********************************************************************************* * apache php uplo ...
随机推荐
- 03 ImageView 图片
四 ImageView 父类 : view >概念:展示图片的控件 >属性: <!-- android:adjustViewBounds=&qu ...
- Oracle Metalink Notes Collection
INV Note 123456.1 Latest 11i Applications Recommended Patch List Note 568012.1:FAQ: Inventory Standa ...
- Android官方命令深入分析之bmgr
作者:宋志辉 bmgr是一个可以跟Backup Manager进行交互的shell工具,要使用这个工具,Android设备API最小为8.它提供了备份和恢复操作的命令,所以你无需频繁的清除数据.这些命 ...
- 查看Linux下的文件
到了这个时候了,也大概的知道了寄出的Linux的操作,是时候接触一下如何查看文件了.我们常用的有以下几种方式: 1.cat,使用cat命令可以将文件的内容输出到显示屏上,也可以将两个文件结合一起输出. ...
- python的文件操作file:(内置函数,如seek、truncate函数)
file打开文件有两种方式,函数用file()或者open().打开后读入文件的内容用read()函数,其读入是从文件当前指针位置开始,所以需要控制指针位置用: 一.先介绍下file读入的控制函数: ...
- C++对象模型(一):The Semantics of Constructors The Default Constructor (默认构造函数什么时候会被创建出来)
本文是 Inside The C++ Object Model, Chapter 2的部分读书笔记. C++ Annotated Reference Manual中明确告诉我们: default co ...
- python,os操作文件,文件路径(上一级目录)
python获取文件上一级目录:取文件所在目录的上一级目录 os.path.abspath(os.path.join(os.path.dirname('settings.py'),os.path.pa ...
- RHEL6安装python包tornado
RHEL6安装python包tornado tornado是使用Python开发的全栈式(full-stack)Web框架和异步网络库,最早由Friendfeed开发.通过使用非阻塞IO,Tornad ...
- 使用HTML5抓取 Audio & Video
原文地址: http://www.html5rocks.com/en/tutorials/getusermedia/intro/ 本地化的文章: http://www.html5rocks.com/z ...
- session效率
(1)-不恰当的request.getSession() 在HttpServlet中,HttpSession对象通常在request.getSession(true)方法调用时才创建. HttpSes ...