4

Is it possible to embed a file attachment in a JSON Object. I have a HTML Form, which has several text field inputs and a file attachment. I want to send a JSON Object wrapping all these form data (including the file attachment) to the server.

Are there any particular libraries in Java available to do that? Can you give possible solution for this.

Thanks

1

2 Answers 2

6

If you want to send the actual data of the file, you'd probably want to encode it as a base64 string and send that in your JSON - see fiddle for example of encoding it in javascript:

http://jsfiddle.net/eliseosoto/JHQnk/

Then you could do the opposite on your server-side using whatever language and/or libraries are appropriate.

Sign up to request clarification or add additional context in comments.

2 Comments

Encoding to base64 will also increase the file size by ~33% but if you are ok with that this is a pretty easy way of getting the job done.
@YuvalBoss I guess you are right. That is a concern. Larger file attachments create bulky JSON object. thats a disadvantage. In terms of the question I have asked, this seems to be the right answer and it works.
1

Use MultipartEntity, someone else posted a similar question: How to send file in JSON on android? You could also consider saving the files on the server and sending a path/url to the file location where the other server can access them.

 public String SendToServer(String aUrl,File Filename)
        {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(filename);

            try 
            {
                MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                entity.addPart("file", new FileBody(Filename));
                entity.addPart("video-title", new StringBody("Video"));
                entity.addPart("video-type", new StringBody("1"));
                httpPost.setEntity(entity);

                HttpContext context = new BasicHttpContext();
                // Bind custom cookie store to the local context
                context.setAttribute(ClientContext.COOKIE_STORE, Globals.sessionCookie);

                HttpResponse response = httpClient.execute(httpPost, context);
                HttpEntity resEntity = response.getEntity();  
                String Response = "";
                if (response != null) 
                {    
                    Response = EntityUtils.toString(resEntity); 
                }
                return Response;
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }

            return "Exception";
        }

1 Comment

I guess in terms of a sustainable solution, uploading the file to a file server and mapping the file url with the request is a good option. Thanks for your suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.