0

我正在尝试将文件从 android 上传到 azure,这让我发疯。我认为它在设置中有些东西,因为我已经确认了在 IIS 中使用 android 运行的代码。现在我可以在 c# 中访问该方法,但我的流在服务器上是空的。在 android 中,我收到 415 错误“类型不匹配”。非常感谢任何帮助!

这是我的 C# 测试客户端:

            ServiceReference1.PostImage2Request r = new ServiceReference1.PostImage2Request();
            MemoryStream memStream = new MemoryStream();                 
            using (FileStream fileStream = File.OpenRead("C:\\crap.png"))
            {
                memStream.SetLength(fileStream.Length);
                fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
            }
            r.image = memStream;
            ServiceReference1.RestServiceImplClient c = new ServiceReference1.RestServiceImplClient();
            ServiceReference1.PostImage2Response p = c.PostImage2(r);

这是我的客户端配置:

 <system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_IRestServiceImpl" />
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="http://testapp.cloudapp.net/Service1.svc"
    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IRestServiceImpl"
    contract="ServiceReference1.IRestServiceImpl" name="BasicHttpBinding_IRestServiceImpl" />
</client>

这是我的安卓代码:

    String url = "https://testapp.cloudapp.net/Service1.svc/postimage2"; 

        String filepath = String.format(Environment.getExternalStorageDirectory() + "/ImageStorage/1.jpg");

        File f = new File(filepath);

        if (f.exists())
        {
            DefaultHttpClient httpclient = new DefaultHttpClient();

            HttpPost httppost = new HttpPost(url);
            ResponseHandler<String> responseHandler = new BasicResponseHandler();

            //This is the new shit to deal with MIME
            MultipartEntity entity = new MultipartEntity();
            entity.addPart("image", new FileBody(f, "multipart/form-data"));

            httppost.setEntity(entity);

            try {
                String responseString = httpclient.execute(httppost, responseHandler);

这是我的服务器代码:

    [OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "PostImage2")]
    string PostImage2(Stream image);

     public string PostImage2(Stream image)
    {
        MultipartParser parser = new MultipartParser(image);

        string content = "testing123";
        try
        {

            if (image == null)
                return "Image stream is null";

            // Read the stream into a byte array
            byte[] data = parser.ToByteArray(image);

            // Copy to a string for header parsing
            content = Encoding.UTF8.GetString(data);

            if (content == "")
                content = "There is no content";

            return content + " - dataLength: " + data.Length.ToString();
        }
        catch (Exception ex)
        {
            return "ERROR" + ex.Message + ex.InnerException + ex.StackTrace;
        }


    }

这是我的服务器配置:

<system.serviceModel>
<behaviors>
  <serviceBehaviors>
    <behavior name="WCFServiceWebRole2.Service1Behavior">
      <!-- To avoid disclosing metadata information, set the value below to false before deployment -->
      <serviceMetadata httpGetEnabled="true"/>
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_WCFServiceWebRole2" transferMode="Streamed" maxBufferSize="10485760" maxReceivedMessageSize="67108864">
      <readerQuotas maxDepth="64" maxStringContentLength="214748364" maxArrayLength="214748364"
          maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <security mode="None">
        <transport clientCredentialType="None" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<services>
  <service name="WCFServiceWebRole2.Service1" behaviorConfiguration="WCFServiceWebRole2.Service1Behavior">        
    <endpoint address="" binding="basicHttpBinding" contract="WCFServiceWebRole2.IRestServiceImpl"/>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>
</services>

4

1 回答 1

0

您使用多部分格式是否有某些特殊原因?通常这样做是因为您想在一个请求中发送多个内容。例如,一个文件和一些相关数据,或几个文件等。您似乎没有这样做,所以多部分格式只是混淆了这个问题。除非您有充分的理由进行多部分格式化,否则我会完全放弃它。让您的客户端发送原始字节,并让服务器代码直接从流中读取,而不使用MultipartParser.

另外,我注意到,一旦您获得图像数据,您将尝试将其读取为 UTF8 数据。我有理由确定这行不通,而且可能不是

于 2013-01-17T16:17:54.097 回答