WCF webHttpBinding包含文件元数据

时间:2011-10-21 05:12:35

标签: android wcf

我正在使用Android客户端将文件上传到wcf服务。 上传工作正常,但现在我想在请求中获取文件名和另一个整数参数。

我该怎么做? afaik,我不能使用消息合同,因为我没有将消息打包为SOAP。还有其他选择吗?

我在Android端使用此代码:

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);

和wcf方面的代码(仅用于测试):

 [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();
        }
    }

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以在URI中传递除Stream之外的其他参数。 http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx上的帖子有一个服务示例。