2011-09-25 178 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