gpt4 book ai didi

android - PHP 到 ASP.NET 代码迁移

转载 作者:行者123 更新时间:2023-11-29 00:30:35 25 4
gpt4 key购买 nike

我正在将图像从我的 android 上传到我的网络服务器。我的 Web 服务器是用 ASP.NET MVC 编写的。

我可以在 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);

我的问题是,是否可以将其转换为我的 ASP.NET MVC?我觉得使用 php 非常有限,因为我不确定如何做一些我可以在 ASP.NET 中做的事情。

我了解 ASP.NET 中的 Request 方法,但不确定如何执行 base64_decode 部分。

附言。有关所用方法的更多信息,请参阅 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);

最佳答案

使用这个例子在 MVC 中上传文件非常简单:

表格:

<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" 启用文件编码。

然后在您的 Controller 中执行此操作:

[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/

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.

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

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.

这是代码示例:

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.

关于android - PHP 到 ASP.NET 代码迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16051030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com