使用Uploadify上传多个文件-输入字段中的需求ID [英] Upload multiple files with Uploadify - need ID from input field
问题描述
我在一页上有多个输入字段.每个输入字段都有一个文本输入和一个文件上载字段.当前的工作流程如下所示:
I have multiple input fields on one page. Each input field have a text input and a file upload field. The current workflow looks like the following:
- 上传后,文件名立即存储在 数据库. (uploadify.php)
- 我从数据库中获取了一个我需要对应的ID 到上传字段. (uploadify.php-在会话中写吗?)
- Directly after the upload the name of the files is stored in the database. (uploadify.php)
- I get an ID from the database which I need to correspond to the upload field. (uploadify.php - write in session?)
现在,我需要一种方法来跟踪哪个文件上传对应于哪个文本输入.如果我具有文件上载字段的ID,则可以与相应的文本输入建立关系,并与数据库返回的ID一起将文本存储到相应的上载.
Now I need a way to keep track of which file upload corresponds to which text input. If I have the id of the file upload field, I can make a relation to the corresponding text input and together with the returned id from the database I could store the text to the corresponding upload.
我尝试了此处所述的方法,但是onSelect
仅具有一个file
对象,该对象不能提供我把输入字段称为Uploadify的数据.在实际版本的Uploadify中,我没有找到另一个提供此功能的函数.
I tried the way described here, but onSelect
only has a file
object which can't provide me the data which input field called Uploadify. I didn't found another function which provide me this in the actual version of Uploadify.
现在我正在考虑为此使用$_SESSION
,但是不知道如何实现.
Now I'm thinking about using $_SESSION
for this, but don't know how this can be done.
客户端:
$('.iconupload').uploadify({
'swf' : '<?php echo SUBFOLDER; ?>/swf/uploadify.swf',
'uploader' : '<?php echo SUBFOLDER; ?>/includes/uploadify.php',
'formData' : {'folder' : '<?php echo IMG_UPLOAD_ICONS; ?>', 'id' : '<?php echo $_SESSION['ID']; ?>', 'type' : 'icon'},
'multi' : false,
'auto' : true,
'removeCompleted': false,
'queueSizeLimit' : 1,
'simUploadLimit' : 1,
'fileTypeExts' : '*.jpg; *.jpeg; *.png; *.gif',
'fileTypeDesc' : 'JPG Image Files (*.jpg); JPEG Image Files (*.jpeg); PNG Image Files (*.png), GIF (*.gif)',
'onUploadError' : function(file, errorCode, errorMsg, errorString) {
alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
},
'onUploadSuccess': function(file, data, response) {
//alert('The file ' + file.name + ' was successfully uploaded with a response of ' + response + ':' + data);
},
'onSelect' : function(file) {
//var elem_id = $(event.target).attr("id"); //here you get the id of the object.
//alert (elem_id);
console.debug(file);
//$("#"+elem_id).uploadifySettings('formData',{'formID':elem_id})
}
});
服务器端:
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];
$res= mkdir(str_replace('//','/',$targetPath), 0750, true);
$res2=move_uploaded_file($tempFile,$targetFile);
$relative_url = str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
echo $relative_url;
if($_REQUEST['type'] == 'icon' && !empty($_REQUEST['id'])){
$iconid = $db->addIcon($_REQUEST['id'], $relative_url);
$icons = array();
if(isset($_SESSION['icons'])){
$icons = $_SESSION['icons'];
}
$icons[$_REQUEST['formID']]= $iconid;
$_SESSION['icons'] = $icons;
}
}
也许可以使用onUploadSuccess
和data
对象,这些对象可以在uploadify.php
中返回并使用一些Javascript魔术.但是我仍然不知道从哪个输入字段上载Uploadify ...
Perhaps one could use onUploadSuccess
and the data
object, which can be returned in uploadify.php
and use some Javascript magic. But I still don't know from which input field Uploadify was called ...
推荐答案
您可以使用jQuery .each()
.这将允许您设置一个ID,以使其通过formData传递,如下所示:
You can setup uploadify using jQuery .each()
. This will allow you to set an id to be passed through the formData like this:
$('.iconupload').each(function() {
var $iconUpload = $(this);
$iconUpload.uploadify({
'swf' : '<?php echo SUBFOLDER; ?>/swf/uploadify.swf',
'uploader' : '<?php echo SUBFOLDER; ?>/includes/uploadify.php',
'formData' : {'folder' : '<?php echo IMG_UPLOAD_ICONS; ?>', 'id' : '<?php echo $_SESSION['ID']; ?>', 'type' : 'icon', 'uploadId': $iconUpload.attr("id")},
'multi' : false,
'auto' : true,
'removeCompleted': false,
'queueSizeLimit' : 1,
'simUploadLimit' : 1,
'fileTypeExts' : '*.jpg; *.jpeg; *.png; *.gif',
'fileTypeDesc' : 'JPG Image Files (*.jpg); JPEG Image Files (*.jpeg); PNG Image Files (*.png), GIF (*.gif)',
'onUploadError' : function(file, errorCode, errorMsg, errorString) {
alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
},
'onUploadSuccess': function(file, data, response) {
//alert('The file ' + file.name + ' was successfully uploaded with a response of ' + response + ':' + data);
}
}
});
如果您不想将数据保存在uploadify.php中,可以将其保存在onUploadSuccess
中,如下所示:
If you do not want to save the data within uploadify.php, you can save it in onUploadSuccess
like this:
'onUploadSuccess': function(file, data, response) {
saveIconData($(iconUpload.attr("id"), file.name);
}
function saveIconData(id, fileName) {
// Save via ajax
}
要通过ajax保存数据,可以使用jQuery的 $.ajax()
方法.您可以将ajax调用设置如下:
To save data via ajax, you can use jQuery's $.ajax()
method. You would setup the ajax call something like this:
$.ajax({
url: "urlToPhpFile.php",
data: { iconID: value, iconFileName: value },
success: function(result) {
// do something on success of save
}
});
这篇关于使用Uploadify上传多个文件-输入字段中的需求ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!