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
Related
In my web application I have a link which, when clicked, invokes an external web service to retrieve a download URL for a file.
I need to send back to client the file which is beyond this URL, instead of the download URL retrieved from the web service. If possible, I would also like to do it without having to download the file on my server beforehand.
I've found this question about a similar task, but which used PHP with the readfile() function.
Is there a similar way to do this in Java 8?
If you doesn't even want to handle that file you should answer the request with a redirect (eg HTTP 301 or 302). If you want to handle the file you should read the file in a byte buffer and send it to the client which would make the transfer slower.
Without seeing your implementation so far, this is my best suggest.
For my web site, I need to get some data from an URL whose response headers contains Content-Disposition attribute which forces me to download the file. I would like to know how I can read the content of the file without downloading the file to disk and do I/O to read it.
Doing so in either Java or JavaScript would be fine.
Content-Disposition is just advisory. If you use a non-browser client (Java, curl, wget...) and do a GET request, you can just do whatever you want.
(I guess this means your question isn't sufficiently specific)
I am trying to upload files using HTML5 File API to a Java Servlet. Here is the use case
A user downloads the file to local file system (using HTML5 file api)
Once, the required change is done, the file needs to be uploaded back to the server.
There is no form based selection and the file needs to be streamed. I am successful in converting files to base64 encoded string and sending to the server (which is form based upload) as a string attribute.
But, I wanted to server to accept Arraybuffer or binary streams. This is not possible in form-based based upload. So, is there any other way of handling file uploads in Java other than form-based methodologies ?
I have a httpservice on my android device. So
I send a single file via XMLHttpRequest, if possible, or iframe in every other case.
If I use Google Chrome it works fine and I have no questions, because it uses XMLHttpRequest to send file.
But when I use IE or Opera which send file via iframe I get a problem, because I receive request like below:
------------Gg3pxYwLKOqlVLkQwq3Hyn
Content-Disposition: form-data; name="test"; filename="Android.png"
Content-Type: image/png
‰PNG
some symbols ....
.......................
------------Gg3pxYwLKOqlVLkQwq3Hyn
I want to parse it and take filename and file content as binary or base64 without header and footer. Can I parse it by Android's instruments or should use 3rd part library? Please, help.
I've found solution: http://commons.apache.org/fileupload/
I'm creating a java web application runing on a Tomcat server.
One of the functions fill in a StringBuffer variable with data.
At the end, I would like to propose the user to download the generated content packaged in a text file. This without having to store the file on the server.
I've been searching for a code snippet but couldn't find anything corresponding ...
I hope I've been clear enough on my problem.
Thanks in advance,
See Making A Download Servlet
Don't forget to add the servlet to your web.xml.
You have to send a content-type along with the response, so that the browser knows what to do with the body of the response.
Normal text has the content-type text/plain, html is text/html. Images are image/gif and so on. For an unknown mime type you normally set "application/octet", which afaik every browser treats as a download. But I recommend to use the propery content type, so the browser might start a matching application to handle the content (e.g. Office for Documents or XML Editor for XML Files ..)
To send a filename along, which the browser suggests for saving, use the following header (example):
Content-Disposition: attachment; filename="downloaded.pdf"
For sending custom headers, use the setHeader() method in the response object.