2011-09-25 179 views
1

我試圖實現HTML5拖放上傳文件。當文件被刪除時,我想調用php文件來處理掉落的文件。我如何調用php文件並訪問php文件中的拖動文件。此外,我想從PHP文件中發回成功或錯誤消息。HTML5拖放問題

我無法弄清楚如何將文件發佈到php並從那裏獲得響應。 到目前爲止我的代碼是:

function drop(evt) { 
     evt.stopPropagation(); 
     evt.preventDefault(); 

     var files = evt.dataTransfer.files; 
     handleFiles(files); 
    } 

    function handleFiles(files) { 

     var file = files[0];  
     var reader = new FileReader(); 

     reader.onload = uploadFile; //main function 
     reader.onloadend = uploadComplete; 
     reader.readAsDataURL(file); 
    } 

    function uploadFile(evt) 
    { 
     //call upload.php 
     //get success msg or error msg 
     //alert(success) or alert(error) 
    } 

這裏的例子upload.php的文件:

<?php 
    $dropped_file //how to get the file 

    if (filesize($dropped_file) > 1024) 
    { 
     echo "size error" //how to send this error 
    } 
    else 
    { 
     echo "success"  //how to send this success msg. 
    } 
?> 

回答

0

您可以使用jQuery,在下落回調執行AJAX調用。

$("body").droppable({ 
    accept: "img", //Your element type goes here e.g. img 
    drop: function(event, ui){ 
     //Perform an AJAX call here. You can access the current dropped item through 
     //ui.draggable 
    } 
)} 
+1

我不建議使用jQuery UI這個下降的能力。很可能有更輕,更清潔的解決方案。 – Bojangles

-1

使用jQuery UI會給你拖動並以最簡單的方式

+0

這不是一個有用的答案。如果您確實相信jQuery UI具有執行拖放操作的一些很棒的功能,那麼您至少可以鏈接到文檔。 – nickf