jQuery表单插件 - 文件上传没有数据?

时间:2010-04-27 11:30:56

标签: jquery jquery-plugins

我一直在努力使用jQuery Form插件。我想创建一个文件上传表单,将数据(JSON,从所选文件)发布到servlet公开的REST服务中。 POST的URL是根据用户在SELECT下拉列表中选择的内容计算的。上传完成后,我想立即通知用户,AJAX风格。

问题是POST标头的Content-Length为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>

1 个答案:

答案 0 :(得分:2)

您的文件输入需要“名称”。