Ich habe Probleme beim Hochladen eines Bildes in eine Web-API, die ich gerade aktiviere. Ich kann Daten von der Web-API abrufen, wenn ich GET-Anfragen verwende, aber ich habe Probleme mit POST-Anfragen. Ich muss ein BMP-Bild in die Web-API hochladen und dann eine JSON-Zeichenfolge zurücksenden.C# webrequest post image to web api
[HttpPost]
public IHttpActionResult TestByte()
{
Log("TestByte function entered");
//test to see if i get anything, not sure how to do this
byte[] data = Request.Content.ReadAsByteArrayAsync().Result;
byte[] test = Convert.FromBase64String(payload);
if(test == null || test.Length <= 0)
{
Log("No Payload");
return NotFound();
}
if (data == null || data.Length <= 0)
{
Log("No payload");
return NotFound();
}
Log("Payload received");
return Ok();
}
Das MVC-Seite, die das Bild sendet sieht wie folgt aus:
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create(url);
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
byte[] byteArray = GetImageData(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, content, barcodeUri));
string base64String = Convert.ToBase64String(byteArray);
byte[] dataArray = Encoding.Default.GetBytes(base64String);
// Set the ContentType property of the WebRequest.
request.ContentType = "multipart/form-data";
// Set the ContentLength property of the WebRequest.
request.ContentLength = dataArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(dataArray, 0, dataArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
Aus irgendeinem Grund habe ich immer eine 404 WebException
bekommen
WebResponse response = request.GetResponse();
ich überprüft haben, dass die URL sein sollte Recht. Formatiere ich die URL für den Post oder mache ich einen anderen Fehler?
bearbeiten, hinzugefügt webconfig Routing:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
Werfen Sie einen Blick [hier] (http://stackoverflow.com/questions/10320232/how-to-accept-a-file-post-asp-net-mvc-4-webapi) –
Sie‘ Um den Inhalt als multipart/form-data zu setzen, aber der Stream nicht als multipart codiert ist, überprüfen Sie dies: http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data – Gusman