2011-10-31 77 views
1

我試圖將mp3歌曲上傳到目錄。但該文件未上傳。我不知道我錯在哪裏。我發送一個示例代碼。使用jquery(jqgrid)上傳MP3文件而不刷新頁面

這是我的代碼

<script type="text/javascript" language="javascript"> 
    jQuery().ready(function(){ 
     jQuery("#list").jqGrid({ 
      url:'music.php', 
      datatype:"xml", 
      mtype:'POST', 
      colNames:[   
      'File Name', 
     'File Upload', 
      colModel:[ 
     {name:'id',index:'id', width:20,editable:false,editoptions {readonly:true,size:25}},         {name:'file_name',index:'file_name', width:-10,sortable: false,hidden:false,editable: true,edittype:"text",editoptions: 

{readonly:true,size:25}},                                 {name:'fileToUpload',index:'fileToUpload',width:-10,editable:true,edittype:'file',editoptions:{size:25},formoptions:{elmprefix:"&nbsp;",elmsuffix:"<button id='buttonUpload'class='button' onclick='return ajaxFileUpload();'>Upload</button><br/>"}},      ], 
      rowNum:10, 
      autowidth: true, 
      rowList:[10,20,30], 
      pager: jQuery('#pager'),sortname:'title',viewrecords: true, sortorder: "desc", caption:"Manage Music",editurl:"music_add.php" 
     }) 
    }).navGrid('#pager',{edit:true,add:true,del:true,view:true}); 

function ajaxFileUpload() { 
var filename = $("#fileToUpload").val(); 
document.getElementById('file_name').value = document.getElementById('fileToUpload').value; 
     $("#buttonUpload").click(function() { 
    $.ajax 
     ({ 
     type: "FILE", 
     url: 'music_add.php', 
     data: {file:filename}, 
       success: function(data){ 
       //alert("Data Uploaded:" + filename); 
      } 
      }); 
    }); 
    return false; 
} 

$("#bedata").click(function(){ 
    jQuery("#list").jqGrid('editGridRow',"new",{height:630,width:350,reloadAfterSubmit:true,closeAfterAdd:true,addCaption:'Add Music',saveData:'saved!'}); 
    $("form#FrmGrid_list") 
    .attr("enctype","multipart/form-data") 
    .attr("encoding","multipart/form-data") 
    ; 
}); 
</script> 
</html> 

PHP代碼:

if ($_POST["oper"] == "add") 
{ 
    $uploaddir = 'uploads/'; 
    $uploadfile = $uploaddir . basename($_FILES['fileToUpload']['name']); 
    if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $uploadfile)) { 
     print('File is valid, and was successfully uploaded. '); 
    } else { 
     echo "Upload error: File may be to large.<br />\n"; 
    } 
    chmod($uploadfile, 0744); 
} 
+0

退房HTML5的新文件上傳功能:http://tutorialzine.com/2011/09/html5-file-upload-jquery-php/ – egasimus

回答

0

使用AJAX你不能上傳文件。您必須通過正常的請求或使用其他選項(如框架或閃光燈)來上傳它。這裏是一個閃光燈的例子:

http://demo.swfupload.org/v220/index.htm

+0

您好感謝ü所以很多爲你的幫助。你可以給出一個文件上傳的示例代碼。 – rlsm123

+0

http://demo.swfupload.org/Documentation/ – egasimus

相關問題