尝试使用以下代码将文件发布到api时:
public async Task UploadAcceptancePageSignaturesAsync(int scheduledWorkId, string acceptanceGuid, string operativeName, bool supervisorSignature, byte[] imageFile)
{
try
{
var content = new MultipartFormDataContent();
content.Add(new StreamContent(new MemoryStream(imageFile)), "\"file\"", "\"image.png\"");
var httpClient = new HttpClient();
var uri = _serverUrl + "api/Acceptance/UploadSignatures/" + scheduledWorkId + "/" + acceptanceGuid + "/" + operativeName + "/" + supervisorSignature;
var uploadServiceBaseAddress = uri;
var httpResponseMessage = await httpClient.PostAsync(uploadServiceBaseAddress, content);
var result = await httpResponseMessage.Content.ReadAsStringAsync();
var x= result;
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
在服务器api端:
var imageFile = HttpContext.Current.Request.Files.Count > 0 ? HttpContext.Current.Request.Files[0] : null;
if (!ModelState.IsValid)
return BadRequest(ModelState);
if (imageFile == null || imageFile.ContentLength <= 0)
return BadRequest("Empty signature image");
imageFile大部分时间都是null,为什么会这样?
谢谢!