【问题标题】:POSTing image from Android to WCF Rest Service将图像从 Android 发布到 WCF Rest 服务
【发布时间】:2011-04-04 17:43:54
【问题描述】:

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

将图像发布到 WCF Rest 服务。我不知道如何配置 WCF Rest Service,你能帮忙吗? 我现在的界面是这样的:

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

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

【问题讨论】:

    标签: c# android wcf rest service


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 请正确格式化您的答案。还可以扩展它以包含 OP 出现问题的原因。
      猜你喜欢
      • 1970-01-01
      • 2012-03-03
      • 1970-01-01
      • 2012-10-11
      • 2013-11-05
      • 2013-01-28
      • 2015-03-07
      • 1970-01-01
      • 2012-03-13
      相关资源
      最近更新 更多