0

formData設定を使用して、uploadifyのクライアント側からサーバーファイルuploadify.phpにデータを渡して変更しようとしています。ここに投稿された多くのソリューションとアップロード フォーラムを試しましたが、役に立ちませんでした。

最初は両方の 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.';
    }
}
4

2 に答える 2

1

問題は思った通りでした。これは、uploadify ボタンの複数のインスタンスを使用し、.uploadifyfile クラスを使用してそれらを参照していたためです。クラスを使用する場合、Uploadify が完全に機能しないようです。

私が思いついたおそらく初歩的な解決策は、「onSelect」関数を使用して、押されたボタンのIDをグローバル変数(window.uploadid)に保存し、これを「onUploadStart」関数で使用することでした。これで、たとえば 2 番目のボタンが押されると、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" />
于 2012-07-21T09:25:44.203 に答える
0

あなたの clientSide コードは正しいです。私は同じコードを使用していますが、うまく機能します。クラスの代わりに id を使用して、uploadify インスタンスを生成することをお勧めします。

于 2013-03-27T04:15:33.427 に答える