PHP,ASP。NET代码迁移

时间:2022-11-07 04:05:27

I'm uploading an image from my android to my web server. My web server is written in ASP.NET MVC.

我正在从我的安卓系统上传一张图片到我的网络服务器。我的web服务器是用ASP编写的。净MVC。

I can upload my image with a HttpPost on the android and then using the following php code:

我可以在android上用HttpPost上传我的图片,然后使用下面的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);

My question is, is it possible to convert this over to my ASP.NET MVC? I feel very limited using the php as I'm not sure how to do a number of things I would be able to do in ASP.NET.

我的问题是,是否有可能将它转换为我的ASP。净MVC吗?我觉得使用php非常有限,因为我不知道如何在ASP.NET中完成很多工作。

I understand the Request method in ASP.NET but I'm unsure how to do the base64_decode part.

我理解ASP中的请求方法。NET,但是我不确定如何做base64_decode部分。

PS. For more information on the method used, see this link

有关所使用方法的更多信息,请参见此链接

Edit: Code for the android part

编辑:android部分的代码

This part converts the bitmap and base64 encodes it

此部分转换位图并对其进行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));

This part does the post

这部分做post

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

2 个解决方案

#1


1  

It is surprisingly simple to upload a file in MVC just use this example:

在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>

Don't forget enctype="multipart/form-data" to enable file encoding.

不要忘记enctype=“multipart/form-data”以启用文件编码。

and then in your controller do this:

然后在控制器中这样做:

[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");
}

Edit: Based on the following blog post: http://blog.tacticalnuclearstrike.com/2010/01/using-multipartentity-in-android-applications/

编辑:基于以下的博客文章:http://blog.tacticaleconstrike.com/2010/01/using-multipartenty -in-android-applications/。

To be able to upload files from an Android application and use the Multipart content type you need to add some additional jar files to your application.

要能够从Android应用程序上传文件并使用多部分内容类型,需要向应用程序添加一些额外的jar文件。

The files needed are apache-mime4j, httpclient, httpcore and httpmime. All are opensource projects built by the Apache foundation.

需要的文件是apache-mime4j、httpclient、httpcore和httpmime。所有这些都是由Apache基金会构建的开源项目。

Download the 4 files and add them to your project then you should be able to use the following code to post strings and files to pages.

下载这4个文件并将它们添加到项目中,然后您应该能够使用以下代码将字符串和文件发布到页面中。

Here is the code example:

下面是代码示例:

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) {
}

The image variable in this case is a File that contains an image captured by the camera on the phone.

在本例中,图像变量是一个文件,其中包含由手机上的摄像头捕获的图像。

#2


1  

So because I was using the base64 encoding method it wasn't working. Had to change my plan of attack and used the MultipartEntity method shown in this answer.

因为我使用的是base64编码方法,所以它不能工作。不得不改变我的攻击计划,并使用了这个答案中所示的多胞胎方法。

Also had to download apache-mime4j, httpclient, httpcore and httpmime. It is now working :-)

还必须下载apache-mime4j、httpclient、httpcore和httpmime。它现在工作:-)

Thanks for your help Mortalus.

谢谢你的帮助,莫塔罗斯。

#1


1  

It is surprisingly simple to upload a file in MVC just use this example:

在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>

Don't forget enctype="multipart/form-data" to enable file encoding.

不要忘记enctype=“multipart/form-data”以启用文件编码。

and then in your controller do this:

然后在控制器中这样做:

[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");
}

Edit: Based on the following blog post: http://blog.tacticalnuclearstrike.com/2010/01/using-multipartentity-in-android-applications/

编辑:基于以下的博客文章:http://blog.tacticaleconstrike.com/2010/01/using-multipartenty -in-android-applications/。

To be able to upload files from an Android application and use the Multipart content type you need to add some additional jar files to your application.

要能够从Android应用程序上传文件并使用多部分内容类型,需要向应用程序添加一些额外的jar文件。

The files needed are apache-mime4j, httpclient, httpcore and httpmime. All are opensource projects built by the Apache foundation.

需要的文件是apache-mime4j、httpclient、httpcore和httpmime。所有这些都是由Apache基金会构建的开源项目。

Download the 4 files and add them to your project then you should be able to use the following code to post strings and files to pages.

下载这4个文件并将它们添加到项目中,然后您应该能够使用以下代码将字符串和文件发布到页面中。

Here is the code example:

下面是代码示例:

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) {
}

The image variable in this case is a File that contains an image captured by the camera on the phone.

在本例中,图像变量是一个文件,其中包含由手机上的摄像头捕获的图像。

#2


1  

So because I was using the base64 encoding method it wasn't working. Had to change my plan of attack and used the MultipartEntity method shown in this answer.

因为我使用的是base64编码方法,所以它不能工作。不得不改变我的攻击计划,并使用了这个答案中所示的多胞胎方法。

Also had to download apache-mime4j, httpclient, httpcore and httpmime. It is now working :-)

还必须下载apache-mime4j、httpclient、httpcore和httpmime。它现在工作:-)

Thanks for your help Mortalus.

谢谢你的帮助,莫塔罗斯。