2014-10-20 35 views
0

我想知道是否可以得到一些建議。jQuery上傳正在運行但未上傳文件

我試圖用jQuery上傳文件,同時保持用戶在同一頁面上。

無論我嘗試沒有工作。我想知道如果有人能看一看,並告訴我哪裏會出錯。

我的HTML是

<form id="import" enctype="multipart/form-data" action="/ajax/postimport" method="POST"> 
    <div class="form-group"> 
     <input type="file" name="filename" id="filename" /> 
    </div> 
    <button type="button" id="importSave">Import</button> 
</form> 

這是我的jQuery

$("#importSave").click(function() 
{ 
    $.ajax({ 
     url: '/ajax/postimport', 
     type: 'POST', 
     dataType: 'json', 
     contentType: false, 
     processData: false, 
     data: {file: $(#filename).val()}, 
     success: function(data) 
     { 
      alert(data.result) 
     }, 
     error: function(textStatus, errorThrown) 
     { 

     } 
    }); 
}); 

,然後我的PHP,這是Laravel 4

 if (Input::hasFile('filename')) { 
      $file = Input::file('filename'); 
      $destPath = public_path() . '/uploads/'; 
      $filename = str_random(10) . '_' . $file->getClientOriginalName(); 
      $uploadSuccess = $file->move($destPath, $filename); 
     } 

回答

0

這是不可能做到這一點。你只需要發佈一個只有一個字段文件的ajax請求:$(#filename).val()。

所以你需要使用ajax上傳庫。 有幾種選擇。但我最喜歡的是

http://www.plupload.com/

http://www.uploadify.com/