Skip to main content

FormData

FormData 用于创建通过 APIRequestContext 发送的表单数据。

🌐 The FormData is used create form data that is sent via APIRequestContext.


方法

🌐 Methods

Append

Added in: v1.44 formData.Append

将一个新值附加到 FormData 对象中已存在的键,或者如果该键不存在则添加该键。文件值可以以 PathFilePayload 的形式传递。可以添加多个同名字段。

🌐 Appends a new value onto an existing key inside a FormData object, or adds the key if it does not already exist. File values can be passed either as Path or as FilePayload. Multiple fields with the same name can be added.

[FormData.Set()](/api/class-formdata.mdx#form-data-set) 和 [FormData.Append()](/api/class-formdata.mdx#form-data-append) 的区别在于,如果指定键已经存在,[FormData.Set()](/api/class-formdata.mdx#form-data-set)会用新键覆盖所有现有值,而 [FormData.Append()](/api/class-formdata.mdx#form-data-append) 会将新值附加到现有值集的末尾。

🌐 The difference between FormData.Set() and FormData.Append() is that if the specified key already exists, FormData.Set() will overwrite all existing values with the new one, whereas FormData.Append() will append the new value onto the end of the existing set of values.

var multipart = Context.APIRequest.CreateFormData();
// Only name and value are set.
multipart.Append("firstName", "John");
// Name, value, filename and Content-Type are set.
multipart.Append("attachment", new FilePayload()
{
Name = "pic.jpg",
MimeType = "image/jpeg",
Buffer = File.ReadAllBytes("john.jpg")
});
// Name, value, filename and Content-Type are set.
multipart.Append("attachment", new FilePayload()
{
Name = "table.csv",
MimeType = "text/csv",
Buffer = File.ReadAllBytes("my-tble.csv")
});
await Page.APIRequest.PostAsync("https://localhost/submit", new() { Multipart = multipart });

用法

FormData.Append(name, value);

参数

返回


Set

Added in: v1.18 formData.Set

在表单上设置一个字段。文件值可以以 PathFilePayload 的形式传递。

🌐 Sets a field on the form. File values can be passed either as Path or as FilePayload.

var multipart = Context.APIRequest.CreateFormData();
// Only name and value are set.
multipart.Set("firstName", "John");
// Name, value, filename and Content-Type are set.
multipart.Set("profilePicture", new FilePayload()
{
Name = "john.jpg",
MimeType = "image/jpeg",
Buffer = File.ReadAllBytes("john.jpg")
});
multipart.Set("age", 30);
await Page.APIRequest.PostAsync("https://localhost/submit", new() { Multipart = multipart });

用法

FormData.Set(name, value);

参数

返回