Sending an html file saved in file system as a response - html

I have developed a small Java Web Application where a user will upload a XML file. Once the user submits the form, the XML file is converted into an html file using XSLT and saved on the file system. Now I want to send this file back to the client/user as a response. How can I do that ?

Read the bytes of the file, and send them to the HTTP response's output stream. You should also set the content type to text/html, and the content length to the length of the file.

Related

Should I use multipart/form-data or text/csv content type for uploading CSV file to server?

I'm working on a back-end feature that involves processing CSV files uploaded by users. Most tutorials I've found so far suggest that I should read that CSV file through a multipart request.
https://www.appcoda.com/restful-api-tutorial-how-to-upload-files-to-server/
How does HTTP file upload work?
However, as far as I know, multipart requests involve a boundary and only make sense when we need to send different kinds of payload over the same request. For CSV file uploading, all I need is to send a byte stream over the request body (with the appropriate text/csv content type).
I'm not sure if there is any specific reason that people use and suggest multipart requests for uploading files?
if you use text/csv your request body should be raw text (like with json) like
"1,2,3
4,5,6"
But if you want upload file, content-type should say about it.
so, in that case i recomend to use multipart/form-data when uploading any type of files

Polymer:how to handle csv file upload and convert to json and send to server

i want to handle a requirement in polymer webcomponents where user can upload csv file from ui and csv file can be parsed to json and sent to server ,i searched and found for vaadin upload,looked over the api but i am not sure how to receive the csv file and convert to json and sent to server,can anyone show a jsfiddle of vaadin upload or any other web component to handle this scenario?
First of all, I am wondering why you would not simply do the conversion on the server side.
In this case, you would be able to use the vaadin-upload directly indeed.
Here is a snippet that would upload all files to the example.com server, and only allow CSV files.
<vaadin-upload target="https://example.com/upload" method="POST" accept="text/csv">
</vaadin-upload>
There are plenty of resources on how to convert CSV files to JSON.
Here is a snippet
And here is a node library
If you really wanted to do the conversion client side, then I would suggest to create an element that would embed a vaadin-upload, and convert the Files array to Json before manually calling the uploadFiles method.

Webservice - Multiple file output formats

I need to create one webservice, that is used to download an audio(wav) file from server by taking input one string id. If audio file is not present at server, i need to send error back in json format.
Now the questions is - How to give the extension for downloaded file. I have no prior idea whether response will be success or failure.
What i am doing now is after dowloading a file - i am changing extension of file using Contet-Type header in response according to audio/wav or application/json.
Is there any way to specify file name with extension from server only. i.e downloaded file with name and extension.
You could set:
Content-Disposition: attachment; filename=FILENAME
header on the server side setting a proper xyz.json or xyz.wav filename as described in RFC 2183.

Send Json inside FormData as File alongside files

I was wondering if it was ok to encode json data as a blob (using atob) alongside some other binary data to the server, and then reload the json file file into memory to save it in the database?
I am doing this in order to prevent multiple posts to the server, I could for example, send the json data, validate it, and when it's OK, I could send the files with some Ids, and then link the both of them server side.
but this method seems broken, because if the user leaves the browser while he is sending the data, and part of the files have been sent, then i'd have alot of uncomplete posts.
Thanks for your answers in advance.

box-api Download a File: how to save raw text response to file with correct encoding

I implemented downloading a file. I received raw string data. I save this data to a file, but can't open it, because file is invalid.
I tried to download jpg image.
I think this is a result of incorrect converting this data to bytes.
What Encoding I need to use?