- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我和我的 friend 在将图片从 Android 应用程序上传到 PHP 中的 Codeigniter 框架时遇到了问题。问题的出现(我们认为)是因为它不是图像文件,因此不能用于图像操作。
一个重要的注意事项是,在使用 html 页面进行测试时,图像上传可以正常工作。所有其他数据(如电子邮件)均在 Android 应用程序中提供并运行。唯一不起作用的是文件上传。但它确实可以与另一个 php 文件一起使用,但我的问题是如何获取最后一个有效的 php 脚本并将其转换为 codeigniter?我想为此使用该框架,但到目前为止我们还无法做到。
这是上传的java代码->
HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost("http://url.com/controller-name");
try
{
CustomMultiPartEntity multipartContent = new CustomMultiPartEntity(new ProgressListener()
{
@Override
public void transferred(long num)
{
publishProgress((int) ((num / (float) totalSize) * 100));
}
});
// We use FileBody to transfer an image
multipartContent.addPart("userfile", new FileBody(new File(filename)));
totalSize = multipartContent.getContentLength();
multipartContent.addPart("email", new StringBody("email@email.com"));
multipartContent.addPart("submit", new StringBody("upload"));
// Send it
httpPost.setEntity(multipartContent);
HttpResponse response = httpClient.execute(httpPost, httpContext);
String serverResponse = EntityUtils.toString(response.getEntity());
Log.i("SERVER", "Response: " + response.toString());
return serverResponse;
}
catch (Exception e)
{
System.out.println(e);
}
codeigniter上传方法:
function do_upload() {
$image_name = time() . $this->get_random_string();
$config = array(
'file_name' => $image_name,
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path . '/thumbs',
'maintain_ratio' => false,
'width' => 300,
'height' => 300
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
echo $this->image_lib->display_errors();
$image_name = $image_name . $image_data['file_ext'];
//Insert into the database
$data = array(
'image_name' => $image_name,
'upload_email' => $this->input->post('email'),
'ip' => $this->input->ip_address()
);
// Insert the new data of the image!
$insert = $this->db->insert('table', $data);
$short_url = $this->alphaID($this->db->insert_id());
return $short_url;
}
旧的 php 脚本可以工作,请注意,注释的代码不起作用。因为它可能会产生与 CI 相同的效果。
<?php
/*if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "application/octet-stream")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{*/
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("images/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"images/" . $_FILES["file"]["name"]);
echo "Stored in: " . "images/" . $_FILES["file"]["name"];
}
}
/*}
else
{
echo "Invalid file";
}*/
?>
请帮助我们修改 CI 函数或 java 代码,以便我们可以上传我们喜爱的图像。感谢您的建议和更好的智慧!
最佳答案
答案就在 java 代码中。我需要向文件体添加更多参数。
multipartContent.addPart("userfile", new FileBody(new File(filename), filename, "image/jpeg", "utf-8"));
这可以完美地工作。您可以在这里找到有关 filebody 构造函数的信息:http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/content/FileBody.html
关于java - 将图片从 Android java 应用程序上传到 Codeigniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9660196/
我想扩展调用 getMessage 时返回自定义消息的异常类。 class MY_Exceptions extends CI_Exceptions{ function __construct
我已经安装了一个干净的 Apache2(加上 PHP 和 MySQL)服务器并启用了 mod_rewrite在 apache 配置中。 我添加了 .htaccess文件以从 url 中删除 index
我正在使用上传类上传图片。但是我上传的图片的存储位置是:http://www.mysite.com/uploads/ 此文件夹的绝对路径是:c:\wamp\www\mysite\uploads\ 应用
大家好 我想在codeigniter上下文中提供一些静态html页面。我不想绕过base_url中的index.php文件。 但是,当我使用对HTML文件的调用时,它不会显示404错误页面。 感谢已经
我一直想知道在模型中以 OO 风格编写代码的正确方法是什么。当然,您可以拥有一个从数据库中检索数据然后映射到模型级变量的函数。但是,当您在模型中有其他功能试图从 BD 获取其他数据时,这种方法会变得违
之前所有的 JOIN 尝试都给我留下了填充结果的 id、标题键的光盘或项目数据(可能发生了冲突)。 所以我有: item table fields: id, title disc table fiel
假设我在 Controller 中有一个名为 的方法 book($chapter,$page); 其中 $chapter 和 $page 必须是整数。要访问该方法,URI 将如下所示 book/cha
我有一个用户可以注册的页面。他在此过程中上传了个人资料照片。我想限制大小,但除了 $config['maxsize'] 之外,并没有太多强调 codeigniter 文档。我尝试了以下但我没有收到任何
我需要将 CodeIgniter 设置为真正的多语言网站。我已经搜索过,但找不到解决方案。 我已经测试了这种方法,但它不起作用。 ( http://codeigniter.com/wiki/Categ
CodeIgniter 中的常量是否可以用于整个站点中的重复文本(比如元标记和元描述)?就像是: define('METADESCRIPTION', 'This is my site'); 然后将 M
我已经在 CodeIgniter 的路由器中写了这个。 $route['companyname'] = "/profile/1"; 这工作正常,但是当我在 URL 中键入“公司名称”时,它不起作用。这
我正在开始我的第一个 CodeIgniter 项目,并希望在开始之前获得一些建议。我对 Controller 和模型的名称如何工作感到有些困惑。 如果我希望我公司页面的网址为 http://examp
可以在CodeIgniter Active Record中使用多个INSERT记录,而无需for,foreach等。 我当前的代码: foreach($tags as $tag) { $tag
SELECT * FROM certs WHERE id NOT IN (SELECT id_cer FROM revokace); 如何在 CodeIgniter 事件记录中编写上述 select
wkhtmltopdf 听起来是一个很好的解决方案...问题是 exec 上没有任何反应 shell_exec("c:\wkhtmltopdf.exe","http://www.google.com
我当前的CodeIgniter有点问题。我有一个带有“页面” Controller 的CI安装程序,该 Controller 可从/ views加载静态文件,但它最多只能包含1个子文件夹,而我正在向其
有一段时间,我一直在处理分页类中的一个问题。 问题是,除了第 1 页的链接之外,所有分页的内容都可以。 所有链接都是这样的: example.com/method/page/2 example.com
我想对请求进行一些预处理和后处理,例如处理身份验证、加载上下文数据、性能时间等等。来自 Django 的概念是 MIDDLEWARE_CLASSES这让我可以在各个阶段处理请求:https://doc
我想通过创建自己的库和配置文件在 CodeIgniter 中生成全局变量。这就是我在我的库文件中编写的,比如说 globalvars.php。我把它放在/application/libraries 中
我有以下分页样式 Previous Page
我是一名优秀的程序员,十分优秀!