NutzCN Logo
问答 fileDownload参数在入口方法适配后为空
发布于 2649天前 作者 bb3239 3083 次浏览 复制 上一个帖子 下一个帖子
标签: nutzwk

我需要从服务器下载一些附件,前端需要传递参数(文件名称:数组),但是在后台入口方法适配后为空。

前端ajax如下:
PS:files是不为空的。

function download(files) {
        var url="${base!}/platform/test/download";
        $.fileDownload(url,{
            data: {filenames:files},
            successCallback: function (url) {

            },
            failCallback: function (html, url) {
                Toast.error("文件下载失败");
            }
        });
    }

后台入口方法如下:

@At
	@Ok("raw")
	@RequiresAuthentication
	public File download(@Param("filenames") String[] filenames, HttpServletRequest req) {

	}
16 回复

估计 req.getParamter("filenames") 也是null

@wendal 论坛不能上传图片,我发到群里了,麻烦你看一下, req.getParamter("filenames")的确是null,但是值确实是有

参数名带中括号了吧,试试这样写

public File download(@Param("filenames[]") String[] filenames, HttpServletRequest req) {

@wendal 解决了,谢谢,这个会不会是fileDownload的原因?

不晓得你用的 fileDownload 是什么东西

@wendal 是jquery的下载插件

http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/

https://github.com/johnculviner/jquery.fileDownload/blob/master/src/Scripts/jquery.fileDownload.js 的247行用的是 $.param

http://api.jquery.com/jquery.param/

如果值是数值, 会自动加上中括号, 神奇的规则

$.param({"abc":["a","2"]})
// 输出 : "abc%5B%5D=a&abc%5B%5D=2"

@wendal 我把字符类型的数组["/upload/test/11.txt","/upload/test/22.xml"]通过Result传到前端,结果变成了11.txt,22.xml,难道浏览器连这种相对路径都不允许吗?如果我一定要这样传,有办法解决吗?谢谢

return Result.success("上传成功",filesPath);

这个filesPath是数组,作为返回值传到前端,结果字符串到前端后由"/upload/test/11.txt"变成了11.txt

前端代码怎么写的

$("#fileinput").on("filebatchuploadsuccess",function (event, data) {
            var filespath=[];
            var filenames=data.filenames;
            for(var i=0;i<data.filescount;i++){
                    filespath.push(filenames[i]);
			}
			var json=JSON.stringify(filespath);
        });

console.log打印出来

------WebKitFormBoundaryIv5nRsMDLHMCotpi
[DEBUG] 2017-08-15 15:43:23,658 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:100) - skip first boundary
[DEBUG] 2017-08-15 15:43:23,658 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:111) - Reading...
[DEBUG] 2017-08-15 15:43:23,660 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) - Upload File info: FilePath=[detail.htm],fieldName=[file_data]
[DEBUG] 2017-08-15 15:43:23,661 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:137) - Upload Info: name=detail.htm,content_type=text/html
[DEBUG] 2017-08-15 15:43:23,683 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) - Upload File info: FilePath=[rrrr.xml],fieldName=[file_data]
[DEBUG] 2017-08-15 15:43:23,685 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:137) - Upload Info: name=rrrr.xml,content_type=text/xml
[DEBUG] 2017-08-15 15:43:23,695 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:231) - ...Done 122569 bytes readed
[DEBUG] 2017-08-15 15:43:23,766 cn.wizzer.app.tunnel.modules.controllers.file.UploadfileController.file(UploadfileController.java:57) - [/upload/tunnel/20170815154323/detail.htm, /upload/tunnel/20170815154323/rrrr.xml]
[DEBUG] 2017-08-15 15:43:26,627 cn.wizzer.app.web.commons.processor.LogTimeProcessor.process(LogTimeProcessor.java:26) - [POST]URI=/file/upload/file 3009ms
[DEBUG] 2017-08-15 15:43:35,018 org.quartz.impl.jdbcjobstore.JobStoreSupport$ClusterManager.manage(JobStoreSupport.java:3877) - ClusterManager: Check-in complete.
[DEBUG] 2017-08-15 15:43:55,018 org.quartz.impl.jdbcjobstore.JobStoreSupport$ClusterManager.manage(JobStoreSupport.java:3877) - ClusterManager: Check-in complete.
[DEBUG] 2017-08-15 15:44:15,001 org.quartz.impl.jdbcjobstore.JobStoreSupport$ClusterManager.manage(JobStoreSupport.java:3877) - ClusterManager: Check-in complete.

我说js的console.log

rrrr.xml apply:269
测试数据.txt apply:269

我想我知道是什么原因了,

$("#fileinput").on("filebatchuploadsuccess",function (event, data) {
            var filespath=[];
            var filenames=data.filenames;
            for(var i=0;i<data.filescount;i++){
                filespath.push(filenames[i]);
                console.log(filenames[i]);
		}
		var json=JSON.stringify(filespath);
        });

这个data并不是Result返回的那个data,而是fileinput上传插件自己返回的data,它里面根本就没有data、msg、code等对象,而是如下所示,要从response中取

data: Object
extra: Object
filenames: Array[2]
files: Array[2]
filescount: 2
form: FormData
jqXHR: Object
reader: FileReader
response: Object
__proto__: Object

非常感谢你的指导

添加回复
请先登陆
回到顶部