[英]Codeigniter 3 - Ajax File Upload (You did not select a file to upload)
[英]CodeIgniter 'You did not select a file to upload' with ajax
有幾個這樣的問題,但我已經檢查了所有這些問題,他們似乎沒有幫助我。 我正在使用CodeIgniter 2.1.4,我正在嘗試使用ajax和文件上傳類上傳文件。 無論我嘗試什么,我總是得到錯誤'你沒有選擇要上傳的文件'。
我把控制器作為:
class Img extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index()
{
$this->load->view('img/index');
}
public function upload()
{
$imgfile = $_POST['imgfile'];
$config['upload_path'] = 'upload/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('imgfile'))
{
$data = array('error' => $this->upload->display_errors('', ''));
$result['success'] = 1;
$result['message'] = $data['error'];
}
else
{
$data = array('upload_data' => $this->upload->data());
$result['success'] = 0;
$result['message'] = "Image Uploaded";
}
die(json_encode($result));
}
並且查看'img / index'為:
<div id="alert" class="alert"> </div>
<form id="addForm" action="" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<input type="file" name="imgfile" id="imgfile" size="20" />
<button type="submit" id="submit_add" name="submit_add">Submit</button>
</form>
<script type="text/javascript">
$("#addForm").submit(function(e)
{
e.preventDefault();
var imgfile = $("#imgfile").val();
$.ajax({
type: "POST",
url: "?c=img&m=upload",
dataType: "json",
data: 'imgfile='+imgfile,
cache: false,
success: function(result){
$('#alert').empty().append(result.message);
}
});
});
</script>
編輯:我在標題中包含malsup.com jQuery表單插件
雖然這個問題很老,但我會提供一個可能有幫助的解決方案。 我被綁起來試圖解決這個問題大約兩周,在這兩周內,我在有限的幫助下掃描了這個問題,直到我找到解決方案。
澄清。
我在使用$ .ajaxFileUpload時遇到了成功我讀到你正在使用malsup。 我必須澄清一下,我使用了它們,而且兩周都沒有工作。
我的問題的解決方案是使用.htaccess。 通過刪除其內容並再次測試應用程序,我發現它確實在URL上使用了index.php。 所以我試圖調查。 我發現我的文件確實已上傳但在重定向期間丟失了。
因此,我建議:
最后你的.htaccess應如下所示
RewriteEngine On
RewriteBase /CI_website/
#CI_website is folder where my codeigniter files are located.
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
您不能只將輸入類型文件的值添加到發布數據中。 這不是這個工作原理。
如果你真的想用async JS上傳你的文件,請查看CodeIgniter和Uploadify 。
更改此行$imgfile = $_POST['imgfile'];
to $imgfile = $_FILES['imgfile'];
。 即便如此,您也無法使用此文件上傳文件,您可以參考此處使用ajax提交表單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.