【发布时间】:2015-06-25 23:18:51
【问题描述】:
参考本教程。 Tracking upload progress with php 。我想让它在 Codeigniter 中工作。我没有开始让它在 CI 中工作。我想上传文件并跟踪进度。
在我的 CI 视图中
<?php $arr = array("id"=>"myform");
echo form_open_multipart("welcome/uploads",$arr); ?>
<input type="hidden" value="myForm" name="<?php echo ini_get("session.upload_progress.name"); ?>">
<table>
<tr>
<td>file</td>
<td><input type="file" name="images[]" multiple></td>
</tr>
<tr>
<td>name</td>
<td><input type="text" name="naam"></td>
</tr>
<tr>
<td></td>
<td><input type="submit"></td>
</tr>
</table>
<?php echo form_close(); ?>
<div id="bar_blank">
脚本
function toggleBarVisibility() {
var e = document.getElementById("bar_blank");
e.style.display = (e.style.display == "block") ? "none" : "block";
}
function createRequestObject() {
var http;
if (navigator.appName == "Microsoft Internet Explorer") {
http = new ActiveXObject("Microsoft.XMLHTTP");
}
else {
http = new XMLHttpRequest();
}
return http;
}
function sendRequest() {
var http = createRequestObject();
http.open("GET", "<?php echo base_url().'index.php/welcome/progress' ?>");
http.onreadystatechange = function () { handleResponse(http); };
http.send(null);
}
function handleResponse(http) {
var response;
if (http.readyState == 4) {
response = http.responseText;
document.getElementById("bar_color").style.width = response + "%";
document.getElementById("status").innerHTML = response + "%";
if (response < 100) {
setTimeout("sendRequest()", 1000);
}
else {
toggleBarVisibility();
document.getElementById("status").innerHTML = "Done.";
}
}
}
function startUpload() {
toggleBarVisibility();
setTimeout("sendRequest()", 1000);
}
(function () {
document.getElementById("myForm").onsubmit = startUpload; //error is here
})();
根据上面的教程,它在核心 php.ini 中。当向控制器dashboard/addRoom 提交表单 CI 请求时,我的页面无论如何都会刷新。但在教程中,Form 重定向到 PHP_SELF(相同的 php 文件)。我对此一无所知。请帮帮我。
控制器
function progress()
{
session_start();
$key = ini_get("session.upload_progress.prefix") . "myForm";
if (!empty($_SESSION[$key])) {
$current = $_SESSION[$key]["bytes_processed"];
$total = $_SESSION[$key]["content_length"];
echo $current < $total ? ceil($current / $total * 100) : 100;
}
else {
echo 100;
}
}
public function uploads()
{
if(!empty($_FILES['images']['name'][0]))
{
//uploadinf file code
}
}
【问题讨论】:
-
我已经使用 codeigniter 中的这个插件完成了文件上传等工作,malsup.com/jquery/form/progress2.html
-
我不想使用插件。
-
你卡在哪里了?你试过了吗?
-
对我来说问题是这样的。当我在 Codeigniter 中提交表单时。然后它进入控制器并刷新页面。那么是否可以运行ajax。
-
显示此表单的控制器和方法的名称是什么?
标签: javascript php codeigniter session