formdata多文件上传_如何使用FormData轻松上传单个或多个文件

formdata多文件上传

In this post, we'll learn about the FormData interface available in modern web browsers as a part of the HTML5 spec.

在本文中,我们将了解HTML5规范中现代Web浏览器中提供的FormData接口。

We'll see examples of using FormData with Ajax, Angular 7, Ionic and React.

我们将看到在Ajax,Angular 7,Ionic和React中使用FormData的示例。

什么是FormData (What's FormData)

FormData is simply a data structure that can be used to store key-value pairs. Just like its name suggests it's designed for holding forms data i.e you can use it with JavaScript to build an object that corresponds to an HTML form. It's mostly useful when you need to send form data to RESTful API endpoints, for example to upload single or multiple files using the XMLHttpRequest interface, the fetch() API or Axios.

FormData只是一个可用于存储键值对的数据结构。 就像它的名字暗示的那样,它是为保存表单数据而设计的,即,您可以将其与JavaScript一起使用以构建与HTML表单相对应的对象。 当您需要将表单数据发送到RESTful API端点时,例如,使用XMLHttpRequest接口, fetch() API或Axios上传单个或多个文件时,它最有用。

You can create a FormData object by instantiating the FormData interface using the new operator as follows:

您可以通过使用new运算符实例化FormData接口来创建FormData对象,如下所示:

const formData = new FormData()

The formData reference refers to an instance of FormData. You can call many methods on the object to add and work with pairs of data. Each pair has a key and value.

formData引用引用FormData的实例。 您可以在对象上调用许多方法来添加和使用数据对。 每对都有一个键和值。

These are the available methods on FormData objects:

这些是FormData对象上可用的方法:

  • append() : used to append a key-value pair to the object. If the key already exists, the value is appended to the original value for that key,

    append() :用于将键值对附加到对象。 如果该键已经存在,则将值附加到该键的原始值,

  • delete()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 NestJS 中,上传多个文件上传单个文件类似,只需要在控制器中稍作修改即可。 以下是一个上传多个文件的示例: 1. 导入 `MulterModule` 并配置: ```typescript import { Module } from '@nestjs/common'; import { MulterModule } from '@nestjs/platform-express'; @Module({ imports: [ MulterModule.register({ dest: './uploads', }), ], }) export class AppModule {} ``` 2. 在控制器中使用 `@UseInterceptors` 装饰器和 `FilesInterceptor` 拦截器来处理上传文件: ```typescript import { Controller, Post, UseInterceptors, UploadedFiles } from '@nestjs/common'; import { FilesInterceptor } from '@nestjs/platform-express'; @Controller('upload') export class UploadController { @Post() @UseInterceptors(FilesInterceptor('files')) // 设置上传文件的字段名 async uploadFiles(@UploadedFiles() files) { console.log(files); // 输出上传文件的信息 } } ``` 这里的 `files` 是上传文件的数组,每个元素都包含上传文件的信息。 3. 在客户端发送文件上传请求。与上传单个文件类似,只需要将多个文件添加到 FormData 中即可。 ```typescript import axios from 'axios'; const formData = new FormData(); formData.append('files', file1); formData.append('files', file2); // 将多个文件添加到 FormData 中 axios.post('http://localhost:3000/upload', formData, { headers: { 'Content-Type': 'multipart/form-data', }, }); ``` 以上就是在 NestJS上传多个文件的基本步骤。注意,上传多个文件时,需要将多个文件都添加到 FormData 中,并使用相同的字段名。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值