Unable to fetch Input File in servlet from request object | Community
Skip to main content
Level 3
Apr 2, 2018 1:59 PM UTC
Solved

Unable to fetch Input File in servlet from request object

Hi Team,

     I have a requirement to fetch the uploaded input file from form and save it into mysql database. Here I am unable to fetch the input file from request object.

My servlet:

@Component(service = Servlet.class, property = {

"service.description=" + "************** Servlet",

"sling.servlet.methods=" + HttpConstants.METHOD_POST,

"sling.servlet.paths=" + "/bin/uploadtestservlet" })

public class UploadTestServlet extends SlingAllMethodsServlet{

@Reference

UploadAdmissionFormService uploadService;

private static final long serialVersionUID = 1L;

private final Logger LOGGER = LoggerFactory

.getLogger(UploadTestServlet .class);

protected void doPost(SlingHttpServletRequest request,

SlingHttpServletResponse response) {

try{

if(ServletFileUpload.isMultipartContent(request)){

List<File> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);

for(File item : multiparts){

LOGGER.info("Name :::"+new File(item.getName()).getName()); //

}

}catch(Exception e){

}

}

My js:

$("#uploadSubmit").click(function(e) {

$.ajax({

type: "POST",

            url: "/bin/uploadAdmissionForm",

            data: 'passport=' +$('#uploadPhoto').get(0).files.item(0),

            success: function(msg) {

           

        },

        });

    });

HTML:

<form method="POST" enctype="multipart/form-data" id="upload-details-form">

<input type="file" name="uploadPhoto" id="uploadPhoto" class="uploadPhoto">

<div class="upload-photo">

<div class="upload-photo-content">

<h4>UPLOAD PHOTO</h4>

<p>Upload your recent passport size (3.5 x 4.5cm) color photograph (format should be jpg, gif, png, jpeg, bmp and maximum file size alloted is 1 MB)</p>

</div>

</div>

<form>

Exception:Exception occurred in doPost :the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded; charset=UTF-8

Even though I have added enctype="multipart/form-data" at form level, this error is getting throw. Can someone please help me here. Thanks

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
smacdonald2008
smacdonald2008Accepted solution
Level 10
Apr 2, 2018 3:14 PM UTC

To handle a file in a Sling Servlet - the syntax is a bit different vs a non-sling Servlet. See this article to learn how to get a file in an AEM Sling Servlet. See the Java code in the doPost method:

Scott's Digital Community: Creating Java Swing applications that post files to AEM ClientLibs folders

Here is another article that shows this use case -- Scott's Digital Community: Uploading files to Adobe Experience Manager DAM using AssetManager API

3 replies

Level 3
Apr 4, 2018 4:20 AM UTC

thanks..now I am able to save the file to mysql database.

Translate
joerghoh
Employee
Apr 2, 2018 7:36 PM UTC

Hi,

Can you use the developer tools of your browser and check that this post is really made with the encoding you want? I don't know HTML that good, but I am not sure if the AJAX call is picking up the encoding you specified at the form itself.

Jörg

Translate
smacdonald2008
smacdonald2008Accepted solution
Level 10
Apr 2, 2018 3:14 PM UTC

To handle a file in a Sling Servlet - the syntax is a bit different vs a non-sling Servlet. See this article to learn how to get a file in an AEM Sling Servlet. See the Java code in the doPost method:

Scott's Digital Community: Creating Java Swing applications that post files to AEM ClientLibs folders

Here is another article that shows this use case -- Scott's Digital Community: Uploading files to Adobe Experience Manager DAM using AssetManager API

Translate