FormData
FormData 用于创建通过 APIRequestContext 发送的表单数据。
¥The FormData is used create form data that is sent via APIRequestContext.
方法
¥Methods
附加
¥Append
Added in: v1.44将新值附加到 FormData 对象中现有的键上,如果键不存在,则添加该键。文件值可以作为 Path
或 FilePayload
传递。可以添加多个同名字段。
¥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() 和 FormData.Append() 的区别在于,如果指定的键已经存在,FormData.Set() 会用新值覆盖所有现有值,而 FormData.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 });
用法
¥Usage
FormData.Append(name, value);
参数
¥Arguments
字段名称。
¥Field name.
文件名
¥File name
MimeType
string
文件类型
¥File type
Buffer
byte[]
文件内容
¥File content
字段值。
¥Field value.
返回
¥Returns
设置
¥Set
Added in: v1.18设置表单上的字段。文件值可以作为 Path
或 FilePayload
传递。
¥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 });
用法
¥Usage
FormData.Set(name, value);
参数
¥Arguments
字段名称。
¥Field name.
文件名
¥File name
MimeType
string
文件类型
¥File type
Buffer
byte[]
文件内容
¥File content
字段值。
¥Field value.
返回
¥Returns