将图像从Android发布到WCF Rest Service

时间:2011-04-04 17:43:55

标签: c# android wcf rest service

您好我使用以下代码: http://vikaskanani.wordpress.com/2011/01/11/android-upload-image-or-file-using-http-post-multi-part/

将图像发布到WCF Rest服务。我不知道如何配置WCF Rest服务,你能帮忙吗? 我当前的界面如下所示:

[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Bare,
           UriTemplate = "SaveImage",
           Method = "POST")]
void SaveImage();

哪个不起作用......可能包含多个错误?

2 个答案:

答案 0 :(得分:1)

这是错的。您应该将Stream参数作为SaveImage方法的参数发送,最好在服务web.config中设置TransferMode =“StreamRequest”。

当POST图像时,在消息正文中使用二进制/八位字节流内容类型和二进制数据。在服务器端 - 从流中读取。

答案 1 :(得分:0)

 using System.ServiceModel;
    using System.ServiceModel.Web;
    using System.IO;
    namespace RESTImageUpload { [ServiceContract] public interface IImageUpload { [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "FileUpload/{fileName}")] void FileUpload(string fileName, Stream fileStream); } }

using System.IO; 
namespace RESTImageUpload
{

public class ImageUploadService : IImageUpload
{

   public void FileUpload(string fileName, Stream fileStream)
    {

        FileStream fileToupload = new FileStream("D:\\FileUpload\\" + fileName, FileMode.Create);

        byte[] bytearray = new byte[10000];
        int bytesRead, totalBytesRead = 0;
        do
        {
            bytesRead = fileStream.Read(bytearray, 0, bytearray.Length);
            totalBytesRead += bytesRead;
        } while (bytesRead > 0);

        fileToupload.Write(bytearray, 0, bytearray.Length);
        fileToupload.Close();
        fileToupload.Dispose();

    }      

}
}