2015-06-17 65 views
0
我有問題,使用C#.NET 這裏從Android應用服務器上傳照片

於Android上傳圖像到服務器是到目前爲止我的代碼...: 客戶端(安卓)使用C#.NET

EDIT

class ImageUploadTask extends AsyncTask<Void, Void, String> { 

    private String webAddressToPost = "http://myIP/Glasses/api/Photo/"; 

    @Override 
    protected String doInBackground(Void... params) { 
     try { 
      HttpClient httpClient = new DefaultHttpClient(); 
      HttpContext localContext = new BasicHttpContext(); 
      HttpPost httpPost = new HttpPost(webAddressToPost); 

      MultipartEntity entity = new MultipartEntity(); 

      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      bitmap.compress(CompressFormat.JPEG, 100, bos); 
      byte[] data = bos.toByteArray(); 
      byte[] file = Base64.encode(data, Base64.DEFAULT); 
      string filecode = new String(file); 

      entity.addPart("uploaded", new StringBody(filecode)); 


      httpPost.setEntity(entity); 
      HttpResponse response = httpClient.execute(httpPost, localContext); 
      BufferedReader reader = new BufferedReader(
        new InputStreamReader(
          response.getEntity().getContent(), "UTF-8")); 

      String sResponse = reader.readLine(); 
      return sResponse; 
     } catch (Exception e) { 
      // something went wrong. connection with the server error 
     } 
     return null;} 

我有在服務器端的一些問題(MVC C#)

[HttpPost] 
    public string SetPhoto([FromBody] string imageSource) 
    { 
     string bytes = imageSource; 
     return "success"; 
    } 

一個d這是我WebApiConfig

config.Routes.MapHttpRoute(
    name: "SetPhoto", 
    routeTemplate: "api/{controller}/{imageSource}"); 
+0

*「該應用程序沒有保存圖像,只是一直崩潰」*任何特定的錯誤/例外? –

回答

0

你需要改變這一部分

returnImage.Save("~/folder1/folder2"); 

要像這樣

returnImage.Save("D:\\folder1\folder2\file.bmp"); 

Image.Save方法期望一個文件的路徑,它需要寫入數據/圖像,欲瞭解更多信息,請訪問MSDN