简体   繁体   English

使用 JavaScript/jQuery 在 Java 中下载生成的文件

[英]Download generated file in Java with JavaScript/jQuery

I am developing web portal, that is based on Java, JavaScript and jQuery.我正在开发 web 门户,它基于 Java、JavaScript 和 ZF590B4FDA3CAF50BE28BZ7C7C8。 JavaScript communicates with Java services through JSON. JavaScript 通过 JSON 与 Java 服务通信。 I need to do this: User want to download file based on the date he choose.我需要这样做:用户希望根据他选择的日期下载文件。 So I need to create hyperlink, on which user can click and the file is generated.所以我需要创建超链接,用户可以点击它并生成文件。 I already have the date and the file (as a byte stream) on Java side, but I don't know how to pass this byte stream as a file to JavaScript side.我已经在 Java 端有日期和文件(作为字节流),但我不知道如何将此字节 stream 作为文件传递给 Z686155AF75A60A0F6E9D80C1F7ED3 端。

If I understand this correctly - you have a server process that can talk JSON to the client and vice versa.如果我理解正确 - 您有一个服务器进程可以将 JSON 与客户端通信,反之亦然。

Now the client requests a file and your return a url to the file现在客户端请求一个文件,您将 url 返回到该文件

I suggest that url to我建议 url 到

a) have the extension of the file type and a) 具有文件类型的扩展名和
b) be served with the headers b) 与标题一起提供

Disposition: attachment; filename=filename.ext;     
Content-type: application/octet-stream

where ext is the extension其中 ext 是扩展名

So return for example the link所以返回例如链接

/filefolder/servefile.jsp?file=page.pdf

and have并且有

Disposition: attachment; filename=page.pdf;     
Content-type: application/octet-stream

in the server process在服务器进程中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM