2013-04-17 161 views
2

我從我的android上傳圖像到我的web服務器。我的web服務器是用ASP.NET MVC編寫的。PHP到ASP.NET代碼遷移

我可以上傳我的形象有HttpPost在Android,然後用下面的PHP代碼:

$base=$_REQUEST['image']; 
$binary=base64_decode($base); 
header('Content-Type: bitmap; charset=utf-8'); 
$file = fopen('App_Data/Image.jpg', 'wb'); 
fwrite($file, $binary); 
fclose($file); 

我的問題是,是否有可能將其轉換到我的ASP.NET MVC?我覺得非常有限的使用PHP,因爲我不知道如何做一些我可以在ASP.NET中做的事情。

我瞭解ASP.NET中的Request方法,但我不確定如何執行base64_decode部分。

PS。對於所使用的方法的詳細信息,請參閱this link

編輯:代號爲Android的部分

這部分轉換的位圖和的base64編碼它

Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"/saved_images/2013-04-10--11-51-33-AEST--Fingerprint.jpg");   
      ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
      bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want. 
      byte [] byte_arr = stream.toByteArray(); 
      String image_str = Base64.encodeBytes(byte_arr); 
      ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
      nameValuePairs.add(new BasicNameValuePair("image",image_str)); 

這部分工作後

 HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://myipaddress/Up/Upload"); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     HttpResponse response = httpclient.execute(httppost); 
+1

你問如何上傳文件或顯示文件? – Mortalus

+0

我可以上傳文件,但我必須把我的ASPNET MVC根文件夾中的一個PHP文件,我想知道是否有可能將此代碼移植到ASP.NET中 – benallansmith

+1

此文件將圖像寫入文件..是你想要什麼?如果您上傳了該文件,爲什麼要重新保存。 – Mortalus

回答

1

在MVC中上傳一個文件只是使用這個例子是令人驚訝的簡單:

FORM:

<form action="controller\UploadImage" method="post" enctype="multipart/form-data"> 

    <label for="file">Filename:</label> 
    <input type="file" name="file" id="file" /> 

    <input type="submit" /> 
</form> 

不要忘記enctype="multipart/form-data"啓用文件編碼。

,然後在你的控制器做到這一點:

[HttpPost] 
public ActionResult UploadImage(HttpPostedFileBase file) { 

    if (file.ContentLength > 0) { 
    var fileName = Path.GetFileName(file.FileName); 
    var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName); 
    file.SaveAs(path); 
    } 

    return RedirectToAction("Index"); 
} 

編輯: 基於以下博客文章:http://blog.tacticalnuclearstrike.com/2010/01/using-multipartentity-in-android-applications/

爲了能夠從一個Android應用程序和使用上傳文件 Multipart內容類型您需要添加一些額外的jar文件到 您的應用程序。

需要的文件是apache-mime4j,httpclient, httpcore and httpmime。 所有這些都是Apache基金會建立的開源項目。

下載4個文件並將它們添加到您的項目,那麼您應該是 能夠使用以下代碼將字符串和文件發佈到頁面。

下面是代碼例如:

HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://www.tumblr.com/api/write"); 


try { 
    MultipartEntity entity = new MultipartEntity(); 

    entity.addPart("type", new StringBody("photo")); 
    entity.addPart("data", new FileBody(image)); 
    httppost.setEntity(entity); 
    HttpResponse response = httpclient.execute(httppost); 
} catch (ClientProtocolException e) { 
} catch (IOException e) { 
} 

在這種情況下的圖像變量是包含由手機上的照相機捕獲的圖像 文件。

+0

感謝您的回覆 - 雖然我知道如何做到這一點,但這只是從MVC內上傳。我從Android應用上傳到我的MVC服務器。 (一個android標籤在主題中,但有人編輯出來......)如果我以這種方式設置我的控制器,並將HttpPost從android指向控制器,它不會識別它。我所能做的就是把上面的php代碼放在我的根目錄中,並將我的HttpPost指向它,它可以工作,但它限制了我所能做的事情。我希望在文件名/文件夾目錄中使用模型變量等 – benallansmith

+1

沒有理由,即使它來自您的Android設備,發佈請求不會對'controller \ UploadImage' URL起作用,您是否可以添加一些日誌和向我們展示發生了什麼,可能有某處出現錯誤,您不必使用PHP來執行此操作。 – Mortalus

+0

我不知道如何正確調試它。我有一個控制器操作幾乎與上面概述的相同。該帖子的android代碼是'HttpPost(「http:// /Up/Upload」);'這是正確的嗎?我有一個成功的try/catch,並執行System.out.println(「Post successful。」+ the_string_response);'返回_post successful_,然後顯示我的索引的html代碼(因爲'return RedirectToAction(「Index」 );')。我知道它的工作原理,因爲我可以上傳使用PHP代碼,所以無論是MVC控制器行動不起作用,或者我指向它錯誤的地址? – benallansmith

1

因此,因爲我使用的是base64編碼方法,它不起作用。不得不改變我的攻擊計劃,並使用this answer.

中顯示的MultipartEntity方法也必須下載apache-mime4j,httpclient,httpcore和httpmime。它現在正在工作:-)

感謝您的幫助Mortalus。