在我知道如何使用/media端点上传图像之后,我想知道如何同时上传多个图像。目前,我使用的代码如下:
const globFiles = []; // an array of files retrieved from file input
const onClick = () => {
return fetch('http://my.host/wp-json/wp/v2/media', {
method: 'POST',
headers: {
'Content-Type': 'image/jpeg',
'X-WP-Nonce': window.nonce,
'Content-Disposition': `attachment; filename="${globFiles[0].name}"`
},
body: globFiles[0]
}).then(response => response.json())
.then((json) => {
console.log(json) // here I receive the media object
}).catch((error) => {
console.error(error);
});
}媒体端点是否能够处理文件数组?我当前代码中的问题是内容配置头,因为我在那里提供了文件名。那么,把它封装在一个for-循环中是可行的吗?
发布于 2018-04-04 17:06:17
虽然不是最优的,但没有什么可以阻止您同时发送两个请求,并在收到两个响应后继续您的逻辑。对于jQuery或JS的承诺,这是相对容易的,但在所有具有异步发送HTTP请求的API的语言中,这很可能是可能的。
请记住,处理请求在CPU资源中是昂贵的,所以要小心,不要试图一次发送1000个图像。
https://wordpress.stackexchange.com/questions/299816
复制相似问题