如何使用uploadify将发布数据发送到服务器端

时间:2014-04-11 07:41:40

标签: javascript php jquery uploadify

这是我的javascript代码。

$(document).ready(function() {
    $("#fileUpload2").fileUpload({
        'method'   : 'GET',
        'uploader': 'js/uploadify/uploader.swf',
        'cancelImg': 'js/uploadify/cancel.png',
        'script': 'model/Properties/Manage Properties/add_files.php',       
        'folder': 'files',
        'multi': true,
        'buttonText': 'Browse',
        'checkScript': 'js/uploadify/check.php',
        'displayData': 'speed',
        'simUploadLimit': 22,
        'OnUploadEvent' : function(dom){
$(dom)('#fileUpload2').uploadifySettings(
    'scriptData', 
    {'ext':$('#osDeed').val(), 'ext2':'bab'}
    );
}

    });
});

此代码只能发送静态数据,但不能发送我不知道的表单数据。我只是被困住请求帮助。

2 个答案:

答案 0 :(得分:0)

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
    //uploadify function
    $("#file_upload").uploadify({
        'uploader': 'uploadify.swf',
        'script': 'uploadify.php',
        'cancelImg': 'cancel.png',
        'folder': 'photos', //folder where images to be uploaded
        'auto': false, // use for auto upload
        'multi': true,
        'queueSizeLimit': 6,
        'buttonImg': 'images/upload.png',
        'width': '106',
        'height': '33',
        'wmode': 'transparent',
        'method': 'POST',
        'scriptData': {'myid':post_id}, //you can post the id here
        'onQueueFull': function(event, queueSizeLimit) {
            alert("Please don't put anymore files in me! You can upload " + queueSizeLimit + " files at once");
            return false;
        },
        'onComplete': function(event, ID, fileObj, response, data) {
            $("#uploadfiles").append(response+",");
        },
        'onAllComplete': function(response, data) {
            showAll();
        }
    });
</script>

scriptData用于将post_id发送到php文件

uploadify.php

if (!empty($_FILES)) {
    $post_id = $_POST['myid'];
    include_once "config.php"; //connect to database

    //use this when uploading images into a folder
    $tempFile = $_FILES['Filedata']['tmp_name'];

    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';

    $fna = $_FILES['Filedata']['name'];
    $targetFile =  str_replace('//','/',$targetPath) . $fna;
    move_uploaded_file($tempFile,$targetFile);
    //folder upload end

           $name2 = mysql_real_escape_string($_FILES['Filedata']['name']);
            $data2 = mysql_real_escape_string(file_get_contents($_FILES['Filedata']['tmp_name']));
            $size2 = intval($_FILES['Filedata']['size']);



        $db->query("INSERT INTO tbl_files SET post_id='$post_id', filename='$name2', file_data='$data2'");

}

答案 1 :(得分:0)

首先,你应该将你的方法从'GET'改为'POST',就像

一样
method = 'post'

您可以通过formData属性通过post发送到服务器端,然后将其更新为uploadify的“onUploadStart”事件 您的最终代码将如下所示

$(document).ready(function() {
$("#fileUpload2").uploadify({
    'formData': {
          'ext': '',
          'ext2':''
    },
    'method'   : 'post',
    'uploader': 'js/uploadify/uploader.swf',
    'cancelImg': 'js/uploadify/cancel.png',
    'script': 'model/Properties/Manage Properties/add_files.php',
    'multi': true,
    'buttonText': 'Browse',
    'checkScript': 'js/uploadify/check.php',
     'onUploadStart': function (file) {
          $('#file_upload').uploadify('settings', 'formData', {'ext': $('#osDeed').val(), 'ext2':'bab'});
      },

     }

   });
});