任何人都曾经成功上传文件到WordPress使用他们的休息API?我可以创建媒体,但内容不上传时,我从WordPress管理检查。这里是代码上传它
public async Task<Author> UpdateProfilePicture(byte[] imageSource, string filename)
{
try
{
var url = Constants.BaseEndpoint + "/media";
var header = ServiceHelpers.GetOauthHeader(url, "POST");
var followCategory = string.Empty;
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Add("Authorization", header);
var requestContent = new MultipartFormDataContent();
var imageContent = new ByteArrayContent(imageSource);
imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
imageContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
FileName = filename + ".jpg"
};
requestContent.Add(imageContent);
var response = await httpClient.PostAsync(url, requestContent);
if (response.IsSuccessStatusCode)
{
var rawResponse = await response.Content.ReadAsStringAsync();
var parsedResponse = JsonConvert.DeserializeObject<Author>(rawResponse);
return parsedResponse;
}
throw new Exception("Not success");
}
}
catch (Exception ex)
{
}
}
字符串
1条答案
按热度按时间sczxawaw1#
WordPress的REST API响应通常很好地表明了它们失败的原因,如果你看一下响应:)这是我从你的代码中得到的:
字符串
删除MultipartFormDataContent -只需发送ByteArrayContent。
型