Reputation: 339
I'm trying to parse an image to a byte array and send it to my web service. Problem is, I couldn't find any way to read the bytearraycontent (in the past I have used HttpContext.Current.Request.Files but obviously it isn't there)... any help please?
EDIT - I managed to get the added form data but it won't save the image properly. I switches to stringContent and it still doesn't work, the received string is exactly the same size as the string I sent and yet it can't open it. Added ' requestValidationMode="2.0" ' in web.config.
code:
public async Task uploadAP()
{
using (var client = new HttpClient())
{
MultipartFormDataContent form = new MultipartFormDataContent();
string str = File.ReadAllText(DEBRIS_PIC_PATH);
form.Add(new StringContent(str), "ap");
HttpResponseMessage response = await client.PostAsync("http://192.168.1.10:8080/WS.asmx/uploadAP", form);
}
}
and obviously something like:
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public void uploadAP()
{
string t = HttpContext.Current.Request.Form["ap"];
FileStream objfilestream = new FileStream(debrisApPath, FileMode.Create, FileAccess.ReadWrite);
objfilestream.Write(binaryWriteArray, 0, binaryWriteArray.Length);
objfilestream.Close();
}
Upvotes: 0
Views: 2590
Reputation: 10184
Apologies for the delay. Here is an example I promised with old-style ASMX web service that will read ByteArrayContent
from a client, following which I will offer two caveats...
using System;
using System.IO;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Collections;
using System.Collections.Specialized;
using System.ServiceModel.Activation;
namespace OldWSTest
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string uploadAP()
{
var foo = HttpContext.Current.Request.Form["ap"];
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(foo);
// do whatever you need with the bytes here
return "done";
}
}
}
I surely echo John Saunders' comments that, for ground-up web service work, a project like this should take a good, hard look at WCF/WebAPI, not ASMX. I had forgotten what a pain ASMX-based web services could be.
I won't promise this is the ideal way to get this data on the web service side; there are almost surely more elegant/efficient/better/slicker/faster ways to do it. I kept finding roadblocks that I think are tied to the limitations of the old-style web service model. This, however, as best I can test, works"
The AspNetCompatibilityRequirements
mode allowed me to access the Form collection, whereas without it, it wasn't available at all without parsing/drilling into the boundary data.
Good luck. I hope this helps.
Upvotes: 1