如何使用jquery和servlet将文件与其他表单属性一起上载到数据库

如何使用jquery和servlet将文件与其他表单属性一起上载到数据库,jquery,html,ajax,servlets,Jquery,Html,Ajax,Servlets,上面的代码片段是我的jquery ajax调用如何将我的所有表单数据发送到servlet,以便我可以访问所有文件内容以及与表单相关的其他属性。在servlet中,我无法访问我的文件内容以及需要上传到数据库的其他表单属性。请向我提供解决方案。 提前感谢。首先,您需要在表单标记中添加enctype=“multipart/form data”作为属性 希望这会对您有所帮助,因为您已经在ajax调用中添加了相同的内容,但只有在表单中已经添加的情况下,它才会起作用 通常,我使用ajaxSubmit API

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

首先,您需要在表单标记中添加enctype=“multipart/form data”作为属性

希望这会对您有所帮助,因为您已经在ajax调用中添加了相同的内容,但只有在表单中已经添加的情况下,它才会起作用

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

祝你好运

请不要忘记使用

$(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);
            }
        });

    });

我以前使用过它,但没有按预期工作
$(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);
            }
        });

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