【问题标题】:How to upload files to database along with other form attributes using jquery and servlets [duplicate]如何使用 jquery 和 servlet 将文件与其他表单属性一起上传到数据库 [重复]
【发布时间】:2015-12-28 01:26:18
【问题描述】:
<div class="modal fade" id="myModal4" tabindex="-1" role="dialog"
        aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog" style="width: 40%;">
            <!-- Modal content-->
            <div class="modal-content">
                <!-- Modal Header-->
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"
                        aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title" id="myModalLabel4">Tender Documents</h4>
                </div>
                <!-- Modal Body-->
                <div class="modal-body">
                    <div class="panel-body">

                        <form id="fileinfo" name="fileinfo"  method="post">

                            <div>
                                <label>Upload File</label><br /> <input type="file" name="file"
                                    size="50" id="file">
                            </div>

                            <div class="form-group">
                                <label>Document Serial No</label><br /> <input type="text"
                                    name="serialNo" id="dSerialNo" class="form-control input-sm"
                                    placeholder="Serial No" required="required">
                            </div>
                            <div class="form-group">
                                <label>Attach Name</label><br /> <input type="text"
                                    name="attachName" id="dAttachName"
                                    class="form-control input-sm" placeholder="Attach Name"
                                    required="required">
                            </div>
                            <div class="form-group">
                                <label>Description</label> <input type="text" id="dDesc"
                                    name="dDescription" />
                            </div>

                            <!-- Modal Footer -->
                            <div class="modal-footer">
                                <button type="button" class="btn btn-info" data-dismiss="modal" id="uploaddocument">Submit</button>
                                <button type="button" class="btn btn-info" data-dismiss="modal">Cancel</button>
                            </div>
                        </form>
                    </div>
                </div>
                <!-- Modal Body-->

            </div>
            <!-- Modal content-->
        </div>
    </div>
    <!-- Modal 4-->

上面的代码sn-p是我的表单,里面有一个表单,用于上传文件,也包含一些属性。

$(document).on('click','#uploaddocument', function(){
        alert("uploaddocument");
        var fd = new FormData(document.getElementById("fileinfo"));
        fd.append("label", "WEBUPLOAD");
        var file=$('#file');
        console.log(file);
        var id=$('.selected').attr('id');
        alert(id);
        var obj={};
        obj.id=id;
        obj.file=$('#file').val();
        obj.serialNo=($('#dSerialNo').val()).toString();
        obj.attachName=$('#dAttachName').val();
        obj.udescription=$('#dDesc').val();

        $.ajax({
            url: '/OtherApplication/upload.do?id='+id,
            type: 'POST',
            dataType: 'json',
            enctype: 'multipart/form-data',
            processData: false,  
            contentType: false ,  
            data: JSON.stringify(obj),
            success: function( data, textStatus, jqXHR) {
                alert("Success");
                console.log(data);
            },
            error: function(jqXHR, textStatus, errorThrown){
                console.log("Something really bad happened " + textStatus);
            }
        });

    });

上面的代码 sn-p 是我的 jquery-ajax 调用如何将我所有的表单数据发送到 servlet,以便我可以访问所有文件内容以及与表单相关的其他属性。在 servlet 中我无法访问我的文件内容还有其他需要上传到数据库的表单属性。请提供解决方案。 提前致谢。

【问题讨论】:

    标签: jquery html ajax servlets


    【解决方案1】:

    首先你需要在你的表单标签中添加 enctype="multipart/form-data" 作为属性。

    希望这会有所帮助,因为您在 ajax 调用中添加了相同的内容,但只有当它具有时才需要。

    通常我使用 ajaxSubmit API 将表单数据处理到 servlet 或 web 服务,这将为您提供保存或更新时在表单中列出的所有对象。

    祝你好运

    请不要忘记使用

     <form id="data" method="post" enctype="multipart/form-data">.
    

    【讨论】:

    • 我之前用过,但没有按预期工作
    猜你喜欢
    • 2013-12-03
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 2015-07-05
    • 2022-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多