ajax上传多个文件与数据,上传多个数据文件时,在ajax调用时从servel获得响应

我在项目中遇到了一些问题。 您好几个jsp和Java类。在一个jsp中,我创建一个只有输入type =“file”和type =“submit”的表单,然后我有一个ajax调用,并将所有formdata发送到我的servel上的doPost类。然后我将该文件发送到数据库,这一切都很好,我的问题是我想从数据库中返回id到.jsp。我可以访问并在doPost上打印以检查我的密钥,但无法将它发送到ajax调用中的成功函数。 这里是我的代码,我非常赞赏任何形式的帮助,谢谢!上传多个数据文件时,在ajax调用时从servel获得响应

ANEXAR FICHEIROS:

Escolher ficheiro para anexar:

的我有我的AJAX调用:

$("#uploadDocuments").submit(function (e) {

alert(10);

alert($("#uploadDocuments").attr('action'));

$.ajax({

type: $("#uploadDocuments").attr('method'),

url: $("#uploadDocuments").attr('action'),

contentType: $("#uploadDocuments").attr("enctype"),

data: new FormData($("#uploadDocuments")[0]),

processData: true,

success: function (data) {

alert("submitDocument");

alert();

/* key = data;

addFilesToTable(key); */

return true;

}

});

e.preventDefault();

$(form).off('submit');

return false;

});

然后我的servlet类:

protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{

response.setContentType("text/html;charset=ISO-8859-1");

PrintWriter out = response.getWriter();

boolean isMultipart = ServletFileUpload.isMultipartContent(request);

ChangeEntityRequestActionBean actionBean = new ChangeEntityRequestActionBean();

if(!isMultipart)

return;

// Create a factory for disk-based file items

DiskFileItemFactory factory = new DiskFileItemFactory();

// Sets the size threshold beyond which files are written directly to

// disk.

factory.setSizeThreshold(MAX_MEMORY_SIZE);

// constructs the folder where uploaded file will be stored

String uploadFolder = getServletContext().getRealPath("") + DATA_DIRECTORY;

// Create a new file upload handler

ServletFileUpload upload = new ServletFileUpload(factory);

// Set overall request size constraint

upload.setSizeMax(MAX_REQUEST_SIZE);

String fileName = "";

Long documentKey = null;

String key = "";

try {

// Parse the request

List items = upload.parseRequest(request);

Iterator iter = items.iterator();

while (iter.hasNext()) {

FileItem item = (FileItem) iter.next();

if (!item.isFormField()) {

fileName = new File(item.getName()).getName();

String filePath = uploadFolder + File.separator + fileName;

File uploadedFile = new File(filePath);

System.out.println(filePath);

// saves the file to upload directory

item.write(uploadedFile);

}

documentKey = actionBean.insertDocument(item, fileName);

System.out.println("Key from DAO ------->>>>>"+documentKey);

key = String.valueOf(documentKey);

}

System.out.println("Key in String from DAO ----->"+key);

System.out.println();

out.println("success");

response.flushBuffer();

}catch (FileUploadException ex) {

throw new ServletException(ex);

} catch (Exception ex) {

throw new ServletException(ex);

} finally {

out.close();

}

}

所有我想要的是送把键值改为通过out.println这样我就可以在jquery函数上使用该值

+0

发送它为JSON哥们。 –

+0

我该怎么做? :/ –

+0

我试图用json发送密钥,它不起作用 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值