【问题标题】:How pass and receive formData values in Uploadify?Uploadify中如何传递和接收formData值?
【发布时间】: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


【解决方案1】:

问题和我想的一样;这是因为我使用了 uploadify 按钮的多个实例,并使用 .uploadifyfile 类来引用它们。使用类时,Uploadify 似乎无法完全正常工作。

我想出的可能是初步的解决方案是使用“onSelect”函数将按下的按钮的 id 存储到全局变量 (window.uploadid) 中,然后在“onUploadStart”函数中使用它。现在,例如,当按下第二个按钮时,fileType 属性成功更改为 finalDetails。

我曾研究过使用 jQuery selectors,但在这种情况下,它们似乎不适用于 id,只是类。

我毫无疑问会对下面的代码进行一些优化,但我希望它能拯救那些与我工作了很多小时的情况相同的人。

<script type="text/javascript">
$(document).ready(function() 
{
    $(".uploadifyfile").uploadify(
    {
        ...
        'method'        : 'post',
        'formData'      : { 'eventDate' : 'notSet', 'eventVenue' : 'notSet', 'fileType' : 'notSet' },
        'onUploadStart' : function(file) 
        {
            var eventDate = "<?php echo $this->row->dates; ?>";
            var eventVenue = "<?php echo JFilterOutput::stringURLSafe($this->row->venue); ?>";
            $(uploadid).uploadify('settings','formData',{ 'eventDate' : eventDate, 'eventVenue' : eventVenue, 'fileType' : fileType });
        },
        'onSelect' : function(event, ID, fileObj)
        {
            alert('event.id:' + event.id);

            var eid = event.id;             // To determine which button was pressed

            if(eid == "SWFUpload_0_0")      // Flyer upload
            {
                window.buttonPressed = "custom01";
                window.uploadid = "#file_upload";
                window.fileType = "flyer";
            }
            else if(eid == "SWFUpload_1_0") // Final Details upload
            {
                window.buttonPressed = "custom02";
                window.uploadid = "#file_upload2";
                window.fileType = "finalDetails";
            }
            ...
        }
    });
});
</script>

...
<input type="file" name="file_upload" id="file_upload" class="uploadifyfile" />
...
<input type="file" name="file_upload2" id="file_upload2" class="uploadifyfile" />

【讨论】:

    【解决方案2】:

    您的客户端代码是正确的。我使用相同的代码,并且效果很好。所以您最好使用 id 来生成 uploadify 实例而不是类

    【讨论】:

      猜你喜欢
      • 2011-09-09
      • 2023-03-07
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-16
      相关资源
      最近更新 更多