简体   繁体   中英

Download generated file in Java with JavaScript/jQuery

I am developing web portal, that is based on Java, JavaScript and jQuery. JavaScript communicates with Java services through JSON. 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.

If I understand this correctly - you have a server process that can talk JSON to the client and vice versa.

Now the client requests a file and your return a url to the file

I suggest that url to

a) have the extension of the file type and
b) be served with the headers

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

where ext is the extension

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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