[英]Sending data to Codeigniter controller from UploadiFive
I am trying to use UploadiFive
to upload some files and, as they are uploaded, add information to a database about them. 我正在尝试使用
UploadiFive
上传一些文件,并在上传时将信息添加到数据库中。 The user enters some details in a form and then clicks upload, at which point the file is uploaded and the information from the form is added to the database with corresponding file name. 用户在表单中输入一些详细信息,然后单击上载,此时上载文件并将表单中的信息添加到具有相应文件名的数据库中。
I've got it working uploading files, but I need the form to post every time a file is completed uploading. 我已经有了上传文件的工作,但每次文件完成上传时我都需要发布表单。 It's posting the form but I'm struggling to get the file name from the uploaded file.
它正在发布表单,但我很难从上传的文件中获取文件名。 Code below:
代码如下:
The HTML page: HTML页面:
<?php echo form_open_multipart('upload/do_upload', 'id="upload_form" name="upload_form"');?>
<div id="queue"></div>
<input id="file_upload" name="file_upload" type="file" multiple="true">
<div id="target"></div>
</form>
<script type="text/javascript">
<?php $timestamp = time();?>
$(function() {
$('#file_upload').uploadifive({
'auto' : true,
'checkScript' : '<? echo base_url();?>uploadify/check-exists.php',
'formData' : {
'timestamp' : '<?php echo $timestamp;?>',
'token' : '<?php echo md5('unique_salt' . $timestamp);?>'
},
'queueID' : 'queue',
'onError' : function(errorType) {
alert('The error was: ' + errorType);
},
'uploadScript' : '<? echo base_url();?>uploadify/uploadifive.php',
'onUploadComplete' : function (event, queueID, fileObj, response, data, file) {
//Post response back to controller
$.post('<?php echo site_url('upload/do_upload');?>', {
field1: $("#field1").val(),
field2: $("#field2").val(),
field3: $("#field3").val(),
field4: $("#field4").val(),
checkbox1: $("#checkbox1:checked").val(),
field5: $("#field5").val(),
filearray : response},
function(info){
$("#target").append(info); //Add response returned by controller
});
}
});
});
</script>
Then my controller: 然后我的控制器:
//Decode JSON returned
$file = $this->input->post('filearray');
$json_decoded = json_decode($file);
// Get the image filename & full filename with path
$image_file = $json_decoded->{'file_name'};
$path = "assets/photos/highres/".$image_file;
echo "IMAGE FILE NAME: " . $image_file; die;
For debugging purposes, I just did an echo of $image_file
. 出于调试目的,我只是回显了
$image_file
。
It seems to be submitting everything except the response from the uploadifive.php
script. 它似乎是提交除
uploadifive.php
脚本的响应之外的所有内容。 When I use Firebug I can see that I do get a response, and it looks correct, but the response ( filearray
) isn't being posted to the form to be decoded. 当我使用Firebug时,我可以看到我得到了响应,看起来是正确的,但响应(文件
filearray
)没有被发布到要解码的表单。
Any ideas as to why I can't get the filename
from the response? 关于为什么我无法从响应中获取
filename
任何想法?
Use event.name
in onUploadComplete:function(event,data){}
在
onUploadComplete:function(event,data){}
使用event.name
onUploadComplete:function(event,data){}
If you really need the server's response, then you might want to use data
(unfortunately, I can't test it but the documentation wouldn't lie, would it ?). 如果你真的需要服务器的响应,那么你可能想要使用
data
(不幸的是,我无法测试它,但文档不会说谎,是吗?)。
The documentation for onUploadComplete
tells us the following: onUploadComplete
的文档告诉我们以下内容:
onUploadComplete
onUploadComplete
Input Type
输入类型
function功能
Overridable
重写
N/AN / A
Triggered once for each file upload that completes.
对于完成的每个文件上载,触发一次。 Arguments
参数
- file
文件
The file object that was uploaded上载的文件对象
- data
数据
The data returned from the server-side upload script (echoed in uploadifive.php)从服务器端上传脚本返回的数据(在uploadifive.php中回显)
Demo
演示
$(function() { $('#file_upload').uploadifive({ 'uploadScript' : '/uploadifive.php' 'onUploadComplete' : function(file, data) { alert('The file ' + file.name + ' uploaded successfully.'); } }); });
This is quite different from what is in your code: 这与您的代码中的内容完全不同:
'onUploadComplete' : function (event, queueID, fileObj, response, data, file) {...}
I could not test UploadiFive
, but did a quick check with Uploadify
: 我无法测试
UploadiFive
,但使用Uploadify
进行了快速检查:
'onUploadComplete' : function(event) {
console.log(JSON.stringify(event,null,4));
}
Which returned this output: 哪个返回了这个输出:
{
"size": 34405,
"post": {},
"modificationdate": "2015-09-21T02:24:51.597Z",
"name": "Tire-wheel-advisor1.jpg",
"creationdate": "2015-09-21T02:24:51.539Z",
"id": "SWFUpload_0_0",
"type": ".jpg",
"filestatus": -4,
"index": 0
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.