2012-01-24 58 views
3

ı想用jquery帖子上傳圖片。我不想使用窗體。用jquery post發佈php圖像上傳

問題是我剛拿到形象的名字,我不能這樣做:

$size=filesize($_FILES['image']['tmp_name']); 

$copied = copy($_FILES['image']['tmp_name'], $newname); 

我的jQuery函數:

$('#add').click(function(){ 

    var image=$('#image').val(); 


    $.post(
      'select.php?p=up', 
      {img:image}, 
      function(answer){ 

       $('#sonuc').html(answer); 
      } 
    ); 

}); 

的html代碼:

<form name="newad" method="post" enctype="multipart/form-data" action="" id="form"> 
<table> 
<tr><td><input type="file" name="image" id="image"></td></tr> 
<tr><td><input name="Submit" type="button" value="Upload" id="add"></td></tr> 
</table>  
</form> 

不是:它的工作原理當我提交表格但它不能像這樣工作,我必須這樣做。

+0

這個問題已經在這裏問: [http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery] [1] [1]:http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery – alfasin

回答

1

我建議爲此使用jsupload插件。你的JavaScript將是:

$(document).ready(function() { 
$("#uploadbutton").jsupload({ 
    action: "addFile.do", 
    onComplete: function(response){ 
     alert("server response: " + response); 
    } 
});