gpt4 book ai didi

javascript - Ajax文件上传返回值的方法

转载 作者:行者123 更新时间:2023-11-28 21:09:23 25 4
gpt4 key购买 nike

我正在使用Ajax upload通过 Ajax 上传文件的插件。它工作正常,文件已成功上传,我也可以在 onComplete 事件中访问其内容。现在我想返回上传文件的数据库 ID。如何将数据库 ID 从 Spring Controller 发送到 onComplete 函数?
这是我的 JS

  new Ajax_upload('#clickToUpload', {
action: '/portal/request/MRNFile/upload',
allowedExtensions: ['csv','jpeg'],
onComplete : function(file){
$('#progress').text('Uploaded ' + file + '. ');
//how to access database id here
}
});

Controller

 @RequestMapping(value = "/upload", method = RequestMethod.POST)
public Integer upload(@RequestParam("userfile") MultipartFile file) {
// This will save file to database and will return its id
return id;
}

谢谢!

最佳答案

通过 echo 或服务器端文件 id 的任何表示进行渲染,它会返回到 JS 中的 file var 中。

如果您想同时返回内容和 ID,请使用

echo json_encode(array('content' => 'content', 'id' => 'yourId'));

在js中通过file = JSON.parse(file);之后的file.contentfile.id访问它>

如果您不使用 PHP,只需返回带有两个键的 JSON 编码数组 - idcontent

关于javascript - Ajax文件上传返回值的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9062241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com