【问题标题】:Javascript problem with form enctype, uploadify script表单 enctype 的 Javascript 问题,uploadify 脚本
【发布时间】:2011-03-02 00:14:32
【问题描述】:

我需要使用uploadify 脚本发送数据时遇到问题。

第一次,uploadify 脚本安装正确,一切正常。但我需要为 mysql 和其他人使用上传的数据(通过 uploadify 脚本)。

这是我调用uploadify脚本的代码:

<script type="text/javascript">
$(document).ready(function() {
$('#file_upload').uploadify({
    'uploader' : '/uplod/uploadify/uploadify.swf',
    'script' : '/uplod/uploadify/uploadify.php',
    'cancelImg' : '/uplod/uploadify/cancel.png',
    'folder' : '/uplod/uploads/',
    'auto' : true,
    'multi' : false,
    'onComplete': function(event, queueID, fileObj, response, data){
        var form = document.forms['form'];
        var i = 0;
        var el = document.createElement("input");
            el.type = "hidden";
            el.name = "test";
            el.id = "test2";
            el.value = fileObj.name;
            form.appendChild(el); },
            'onAllComplete': function(event,data) { document.getElementById('form').submit();},
 });
});
</script> 

我在以下位置找到此代码:http://www.uploadify.com/forums/discussion/7102/for-all-those-who-struggle-on-inserting-filenames-into-a-db-after-uploading/p1

文件上传后如何回调。

这里是uploadify.php代码:

<?php
   if (!empty($_FILES)) {
   $tempFile = $_FILES['Filedata']['tmp_name'];
   $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
   $targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
   move_uploaded_file($tempFile,$targetFile);
   echo '0'; // Required to trigger onComplete function on Mac OSX (same for linux and        windows as it seems)

   }
   else { // Required to trigger onComplete function on Mac OSX (same for linux and windows as it seems)
   echo '1';
   $video = $_POST[test];
   $textfield = $_POST[textfield];
   echo $video;
   echo $textfield;
   }
  ?>

这是我需要创建的表格,我如何才能完成这项工作。

  <form id="form" name="form" method="post" enctype="multipart/form-data" action="uploadify/uploadify.php">
  <input id="file_upload" name="file_upload" type="file" />
  <input type="text" name="textfield" id="textfield" />
  </form>

问题就在这里,当我使用enctype="multipart/form-data"uploadify.php 给我写0,当我删除enctype="multipart/form-data" 给我写1。这就是我需要的。从代码中可以看出,我在uploadify.php的末尾放了一些愚蠢的代码来测试它。

我认为这不起作用,因为在 javascript 代码中,我不会在任何地方编写 enctype。也许我错了。 我试着编辑这个:

            var form = document.forms['form'].enctype  = "multipart/form-data"; 

什么都没有,

我也试过了:

'onAllComplete': function(event,data) { document.getElementById('form').enctype; document.getElementById('form').submit();},

但又没有。欢迎所有帮助...

P.S 这是测试示例,对于此代码,我不需要 multipart/form-data 但在测试的项目中,我需要 multipart/form-data 因为我正在测试小型视频上传器的 uploadify 脚本,我需要每个视频的封面图片。

【问题讨论】:

    标签: php javascript jquery uploadify


    【解决方案1】:

    试试:

    var form = document.forms['form'];
        form.enctype  = "multipart/form-data"; 
    

    而不是

    var form = document.forms['form'].enctype  = "multipart/form-data"; 
    

    但由于您不使用多次上传,我认为不需要分两步执行此操作(首先发送文件,然后发送完整表格)。 请参阅scriptData-选项,它允许您发送文件和附加数据:

    'scriptData'  : {'textfield':$('#textfield').val()}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多