跳到主要内容

FormData

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


方法

Append

添加于:v1.44 formData.Append

将新值追加到 FormData 对象中现有键上,如果键不存在则添加该键。文件值可以作为 Path 或作为 FilePayload 传递。可以添加多个同名字段。

FormData.Set()FormData.Append() 之间的区别在于,如果指定的键已经存在,FormData.Set() 将用新值覆盖所有现有值,而 FormData.Append() 会将新值追加到现有值的末尾。

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

添加于:v1.18 formData.Set

在表单上设置一个字段。文件值可以作为 Path 或作为 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);

参数

返回值