问题
I need to receive a json object together with a byte array in a c# Web API application.
This is how I am sending the data:
public bool SendMedia(string method, Media media)
{
string filePath = Path.GetFullPath(Path.Combine(filesDirectory, media.FileName));
if (!File.Exists(filePath))
{
return false;
}
using (var client = new HttpClient())
using (var content = new MultipartContent() )
{
content.Add(new StringContent(JsonConvert.SerializeObject(media), Encoding.UTF8, "application/json"));
byte[] b = File.ReadAllBytes(filePath);
content.Add(new ByteArrayContent(b, 0, b.Length));
var response = client.PostAsync(new Uri(baseUri, method).ToString(), content).Result;
if (response.IsSuccessStatusCode)
return true;
return false;
}
}
And this is how I am trying to receive it:
// POST: api/Media
[ResponseType(typeof(Media))]
public HttpResponseMessage PostMedia(Media media, byte[] data)
{
int i = data.Length;
HttpResponseMessage response = new HttpResponseMessage();
if (!ModelState.IsValid)
{
response.StatusCode = HttpStatusCode.ExpectationFailed;
return response;
}
if (MediaExists(media.MediaId))
WebApplication1Context.db.Media.Remove(WebApplication1Context.db.Media.Where(p => p.MediaId == media.MediaId).ToArray()[0]);
WebApplication1Context.db.Media.Add(media);
try
{
WebApplication1Context.db.SaveChanges();
}
catch (DbUpdateException)
{
response.StatusCode = HttpStatusCode.InternalServerError;
return response;
throw;
}
response.StatusCode = HttpStatusCode.OK;
return response;
}
I don't know much about developing for web at the moment. Is sending a MultipartContent the right approach?
回答1:
The framework can only bind one item from the body so what you are trying to attempt would not work.
Instead, read the request content just as you sent it and extract the parts.
[ResponseType(typeof(Media))]
public async Task<IHttpActionResult> PostMedia() {
if (!Request.Content.IsMimeMultipartContent()) {
return StatusCode(HttpStatusCode.UnsupportedMediaType); }
var filesReadToProvider = await Request.Content.ReadAsMultipartAsync();
var media = await filesReadToProvider.Contents[0].ReadAsAsync<Media>();
var data = await filesReadToProvider.Contents[1].ReadAsByteArrayAsync();
int i = data.Length;
if (!ModelState.IsValid) {
return StatusCode(HttpStatusCode.ExpectationFailed);
}
if (MediaExists(media.MediaId))
WebApplication1Context.db.Media.Remove(WebApplication1Context.db.Media.Where(p => p.MediaId == media.MediaId).ToArray()[0]);
WebApplication1Context.db.Media.Add(media);
try {
WebApplication1Context.db.SaveChanges();
} catch (DbUpdateException) {
return StatusCode(HttpStatusCode.InternalServerError);
}
return Ok(media);
}
Note also that in your original code you state the the action has a [ResponseType(typeof(Media))] but an object of that type was never returned. The above answer includes the model in the Ok(media) response.
The is a very simplified example. add any validation as necessary.
来源:https://stackoverflow.com/questions/43188759/how-to-receive-a-byte-array-and-json-in-a-web-api-controller