I am using an Android client to upload files to a wcf service.
The upload is working, but now I want to get the name of the file and another integer parameter into the request.
How can I do this? afaik, I can not use a message contract since I am not packaging the message as SOAP. Is there an alternative?
I use this code on the Android side:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(serviceAddress +
"/Upload/");
ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("step", new StringBody("1"));
reqEntity.addPart("fileName", new StringBody("elad.jpg"));
reqEntity.addPart("file", bab);
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
and this code on the wcf side (just for teesting):
[WebInvoke(UriTemplate = "", Method = "POST", BodyStyle= WebMessageBodyStyle.Bare)]
public void Upload(Stream fileStream)
{
FileStream targetStream = null;
string uploadFolder = @"C:\inetpub\wwwroot\Upload\test.jpg";
using (targetStream = new FileStream(uploadFolder, FileMode.Create,
FileAccess.Write, FileShare.None))
{
const int bufferLen = 65000;
byte[] buffer = new byte[bufferLen];
int count = 0;
while ((count = fileStream.Read(buffer, 0, bufferLen)) > 0)
{
targetStream.Write(buffer, 0, count);
}
targetStream.Close();
fileStream.Close();
}
}
Thank you!
You can pass an additional parameter other than the
Streamone in the URI. The post at http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx has an example of a service with that.