2010-04-27 79 views
2

我一直在用jQuery Form插件掙扎。我想創建一個文件上傳表單,將數據(來自所選文件的JSON)發佈到由servlet公開的REST服務中。 POST的URL根據用戶在SELECT下拉列表中選擇的內容計算得出。上傳完成後,我想立即通知用戶,AJAX風格。jQuery表單插件 - 文件上傳沒有數據?

問題是POST標頭的內容長度爲0並且不包含任何數據。我將不勝感激任何幫助!

<html> 
<head> 
<script type="text/javascript" src="js/jquery-1.4.2.min.js">/* ppp */</script> 
<script type="text/javascript" src="js/jquery.form.js">/* ppp */</script> 

<script type="text/javascript"> 

function cb_beforesubmit (arr, $form, options) { 
    // This should override the form's action attribute 
    options.url = "/rest/services/" + $('#selectedaction')[0].value; 
    return true; 
} 

function cb_success (rt, st, xhr, wf) { 
    $('#response').html(rt + '<br>' + st + '<br>' + xhr); 
} 

$(document).ready(function() { 
    var options = { 
     beforeSubmit: cb_beforesubmit, 
     success: cb_success, 
     dataType: 'json', 
     contentType: 'application/json', 
     method: 'POST', 
    }; 
    $('#myform').ajaxForm(options); 

    $.getJSON('/rest/services', function (data, ts) { 
     for (var property in data) { 
      if (typeof property == 'string') { 
       $('#selectedaction').append('<option>' + property + '</option>'); 
      } 
     } 
    }); 

}); 

</script> 

</head> 
<body> 
<form id="myform" action="/rest/services/foo1" method="POST" enctype="multipart/form-data"> 
<!-- The form does not seem to submit at all if I don't set action to a default value? !--> 
<select id="selectedaction"> 
</select> 
<input type="file" value="Choose"/> 
<input type="submit" value="Submit" /> 
</form> 

<div id="response"> 
</div> 

</body> 
</html> 

回答

2

您的文件輸入需要一個「名稱」。

+0

謝謝!這樣做:) – pojo 2010-04-28 08:47:57

相關問題