【发布时间】:2012-06-03 14:42:29
【问题描述】:
我一直在尝试使用 formData 设置将数据从 uploadify 的客户端传递和修改到服务器文件 uploadify.php。我已经尝试了这里和uploadify 论坛上发布的许多解决方案,但都无济于事。
最初,两个 formData 值都设置为字符串“empty”,然后当开始上传时,eAlias 设置为 2,eDate 设置为日期。然后,服务器脚本通过 POST 接收这些值并将它们回显到客户端脚本,客户端脚本在警报中显示此数据(在 onUploadSuccess 中)。在尝试的所有可能的解决方案中,值要么是“”,要么是“空”,即 onUploadStart 中 formData 键的设置不起作用。
我已经在下面包含了大部分客户端脚本和服务器脚本。
任何帮助或建议将不胜感激,谢谢。
客户端脚本:
<script type="text/javascript">
$(document).ready(function()
{
$(".uploadifyfile").uploadify(
{
'swf' : '/xx/uploadify.swf',
'uploader' : '/xx/uploadify.php',
'auto' : true,
'height' : 15,
'method' : 'POST',
'multi' : false,
'uploadLimit' : 10,
'formData' : { 'eAlias' : 'empty', 'eDate' : 'empty' },
'onUploadSuccess' : function(file, data, response)
{
alert('The file ' + file.name + ' was successfully uploaded with a response of ' + response + ' : ' + data);
document.getElementById("adminForm")[buttonPressed].value = data;
},
'onUploadStart' : function(file)
{
var eventDate = "<?php echo $this->row->dates; ?>";
var eventVenue = 'test';
alert('Venue Alias: ' + eventVenue + '\neventDate: ' + eventDate);
//**** The line below is the one in question ****//
$(".uploadifyfile").uploadify("settings", "formData", {"eAlias": 2, "eDate" : eventDate});
},
'onSelect' : function(event, ID, fileObj)
{
var eid = event.id;
if(eid == "SWFUpload_0_0")
{
window.buttonPressed = "custom01";
alert('1');
}
...
}
});
});
</script>
服务器端脚本
$targetFolder = '/xx/uploads'; // Relative to the root
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];
// Set $someVar to 'someValue'
$eventAlias = $_POST['eAlias'];
$eventDate = $_POST['eDate'];
// Validate the file type
$fileTypes = array('jpg','jpeg','gif','png'); // File extensions
$fileParts = pathinfo($_FILES['Filedata']['name']);
if (in_array($fileParts['extension'],$fileTypes)) {
move_uploaded_file($tempFile,$targetFile);
echo $targetFolder . '/' . $_FILES['Filedata']['name'];
echo ' eventAlias: '.$eventAlias.' eventDate: '.$eventDate;
} else {
echo 'Invalid file type.';
}
}
【问题讨论】:
-
您真的应该使用 FireBug 或 Chrome 的 JavaScript 控制台对其进行调试。您是否通过简单/演示示例使其工作?
-
您的代码似乎对我有用,可能问题出在另一个代码块中。请尝试一个最简单的代码来发布一些东西,你会看到
-
你确定这条线正确吗 var eventDate = "row->dates; ?>";可能会出错,导致代码不工作,尝试调试 eventDate 变量
-
@Alexander 我尝试使用 FireBug 对其进行调试,这让我进入了现在的阶段。
-
@TrinhHoangNhu 我原以为它也能正常工作。我将它用于 Joomla 站点,而 Joomla 往往会把事情搞得一团糟,但我已经设法让脚本的其他部分正常工作。是的,我确定该行是正确的,它返回页面所关注的事件的日期,但是,我将对其进行调试,看看会发生什么。
标签: php javascript jquery upload uploadify