0
我想從Android(2.3.4)使用下面的代碼上傳文件,我知道我必須使用多部分後,如果我也想發送參數。這是我的Android代碼:上傳文件從Android(2.3.4)到WCF 4.0 REST服務與WCF模板40
public void uploadFile(File uploadFile) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://192.168.0.102/application/services/sendfile/");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
// Indicate that this information comes in parts (text and file)
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
try {
//Create a JSON object to be used in the StringBody
JSONObject jsonObj = new JSONObject();
//Add some values
jsonObj.put("filename", uploadFile.getName());
//Add the JSON "part"
reqEntity.addPart("entity", new StringBody(jsonObj.toString()));
}
catch (JSONException e) {
Log.v("App", e.getMessage());
}
catch (UnsupportedEncodingException e) {
Log.v("App", e.getMessage());
}
FileBody fileBody = new FileBody(uploadFile);//, "application/octet-stream");
reqEntity.addPart("file", fileBody);
try {
postRequest.setEntity(reqEntity);
//Execute the request "POST"
HttpResponse httpResp = httpClient.execute(postRequest);
//Check the status code, in this case "created"
if(((HttpResponse) response).getStatusLine().getStatusCode() == HttpStatus.SC_CREATED){
Log.v("App","Created");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
它總是檢索錯誤的請求。
在另一邊,我在使用模板40 WCF REST服務提出:
[WebInvoke(
Method = "POST",
UriTemplate = "sendFile",
BodyStyle = WebMessageBodyStyle.Wrapped)]
public int sendFile(Stream receivedFile)
{
// How should I process the multipart file here to save it with its name on disk?
}
我在我的web.config
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
<directoryBrowse enabled="false" />
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<standardEndpoints>
<webHttpEndpoint>
<!--
Configure the WCF REST service base address via the global.asax.cs file and the default endpoint
via the attributes on the <standardEndpoint> element below
-->
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" />
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
有這樣的,這在我的Global.asax
private void RegisterRoutes()
{
RouteTable.Routes.Add(new ServiceRoute("Service", new WebServiceHostFactory(), typeof(AccidentService)));
}
任何想法爲什麼WCF沒有收到文件,如果我把一個斷點?
如果任何人有更好的例子來說明如何從Android(2.3.4)發送文件到WCF 4.0將非常感激。
在此先感謝!吉列爾莫。
您可以與我們分享您的更改嗎?謝謝。 – VansFannel 2012-05-29 06:24:40
-1因爲「改變他們使它工作」缺少_how_他們被改變。 – AD7six 2014-03-01 18:58:29