2012-04-19 45 views
0

嘗試上傳圖片時出現以下錯誤。使用Android的WCF服務上傳圖片:因內容類型無法處理

無法處理消息,因爲內容類型'multipart/form-data; border = AwQm1pbogJ6qQuZlUZjJ6kNOvbehrlyozA-w'不是預期的類型'text/xml;字符集= UTF-8' 。

的Android代碼:

HttpPost httppost = new HttpPost("http://192.168.1.111/androidservice/MediaUploadService.svc/uploadFile"); 
File photo = new File(Environment.getExternalStorageDirectory(), "01.jpg"); 
MultipartEntity t = new MultipartEntity(); 
t.addPart("t", new FileBody(photo)); 
httppost.setEntity(t); 
HttpClient httpclient = new DefaultHttpClient(); 
HttpResponse response = httpclient.execute(httppost); 

WCF代碼:

namespace AndroidService 
{ 
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "MediaUploadService" in code, svc and config file together. 
public class MediaUploadService : IMediaUploadService 
{ 
    public void UploadFile(Stream fileContents) 
    { 
     byte[] buffer = new byte[10000]; 
     int bytesRead, totalBytesRead = 0; 
     do 
     { 
      bytesRead = fileContents.Read(buffer, 0, buffer.Length); 
      totalBytesRead += bytesRead; 
     } while (bytesRead > 0); 
     File.WriteAllText(@"D:\\Vechile\log2.txt", totalBytesRead.ToString()); 
    } 
} 
} 

綁定:

<system.serviceModel> 
<behaviors> 
    <endpointBehaviors> 
    <behavior name="httpBehavior"> 
     <webHttp /> 
    </behavior> 
    </endpointBehaviors> 

    <serviceBehaviors> 
    <behavior name=""> 
     <serviceMetadata httpGetEnabled="true" /> 
     <serviceDebug includeExceptionDetailInFaults="false" /> 
    </behavior> 

    <behavior name="PublishMetadataBehavior"> 
     <serviceMetadata httpGetEnabled="true" policyVersion="Policy15"/> 
    </behavior> 

    </serviceBehaviors> 
</behaviors> 

<bindings> 
    <webHttpBinding> 
    <binding name="WebHttpDtreaming" transferMode="Streamed" > 
    </binding> 
    </webHttpBinding> 
</bindings> 

<standardEndpoints> 
    <webHttpEndpoint> 
    <standardEndpoint name="" 
        helpEnabled="true" 
        automaticFormatSelectionEnabled="true" 
        maxReceivedMessageSize="1000000"/> 
    </webHttpEndpoint> 
</standardEndpoints> 

<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" /> 

我將不勝感激任何幫助。

+0

你能告訴我們你的IMediaUploadService( 「內容類型」, 「應用/ JSON」)? – 2012-12-01 14:00:55

回答

0

試試這個 httppost.setHeader