2015-03-02 56 views
-1

我想用ajax上傳視頻文件。但下面的代碼不起作用。當我點擊上傳按鈕,我得到一個瀏覽器錯誤說:無法使用ajax上傳視頻

Uncaught TypeError: Illegal invocation 

$('#uploadmatch').click(function(event) { 
            var formData = new FormData($('form')[0]); 
       event.preventDefault(); 
       $.ajax({ 
        url : 'http://localhost:8081/Football/UploadMatch', 
        data : formData, 
        //processData : false, 
        type : 'POST' 
       }) 
            .done(function(message) { 
             alert(message); 
            }) 
            .fail(function(message){ 
             alert(message); 
            }) 
}); 

我要去哪裏錯了?

+0

@Trupti你以爲?你是否驗證了屬性URL的值? – 2015-03-02 08:07:52

回答

0

我認爲問題是通過數據。我測試過你的代碼,formData對象似乎是問題所在。你用錯了。 FormData有一個你必須使用的append。我發現了一個很好的例子,說明如何通過AJAX上傳文件(http://abandon.ie/notebook/simple-file-uploads-using-jquery-ajax),並且他們使用的是FormData。

下面是代碼示例:

首先你準備的文件

// Variable to store your files 
var files; 

// Add events 
$('input[type=file]').on('change', prepareUpload); 

// Grab the files and set them to our variable 
function prepareUpload(event) 
{ 
    files = event.target.files; 
} 

然後AJAX調用:

$('form').on('submit', uploadFiles); 

// Catch the form submit and upload the files 
function uploadFiles(event) 
{ 
    event.stopPropagation(); // Stop stuff happening 
    event.preventDefault(); // Totally stop stuff happening 

    // START A LOADING SPINNER HERE 

    // Create a formdata object and add the files 
    var data = new FormData(); 
    $.each(files, function(key, value) 
    { 
     data.append(key, value); 
    }); 

    $.ajax({ 
     url: 'submit.php?files', 
     type: 'POST', 
     data: data, 
     cache: false, 
     dataType: 'json', 
     processData: false, // Don't process the files 
     contentType: false, // Set content type to false as jQuery will tell the server its a query string request 
     success: function(data, textStatus, jqXHR) 
     { 
      if(typeof data.error === 'undefined') 
      { 
       // Success so call function to process the form 
       submitForm(event, data); 
      } 
      else 
      { 
       // Handle errors here 
       console.log('ERRORS: ' + data.error); 
      } 
     }, 
     error: function(jqXHR, textStatus, errorThrown) 
     { 
      // Handle errors here 
      console.log('ERRORS: ' + textStatus); 
      // STOP LOADING SPINNER 
     } 
    }); 
} 

我希望這可以幫助你,無論如何。讓我知道。

乾杯!