Create Folder DAM y send multiple images

kevinl17371805 13-12-2018


I am trying to create a folder and save multiple images in the DAM

I have based on that example of the link but I have the inconvenience when creating the folder and at the time of trying to go through all the images that come to me in the servlet I hope to have made me understand and I appreciate the help that you can give me

Mark Solution

Are these answers useful?
Help other community members by marking useful answers as accepted.

Accepted Solutions (0)

Answers (4)

Answers (4)



   I understand that you are trying to upload multiple images to the DAM. I don't see any issue in the Java code. Are you sure all the images are being retrieved inside the Servlet ? Can you log and see what all files you are receiving in the servlet ? Also , if you can send a sample project you are working on , may be I can check and resolve and give you back the solution



kevinl17371805 14-12-2018

Hello edubey

Ok let me explain a little better I would like to know whether you can create a folder on the dam from a SlingServlet each to save a new group of images. The other downside is that at the time of saving several images with the code that I'm just being able to save the first image of all that is being sent and best said was for me to work with an example in which I can base to create the folder and to be able to obtain all the images that I am trying to save add code of servlet and AJAX that I am using thanks


@SlingServlet(paths = { "/bin/store/tools/addFiles" }, methods = { "POST" })

public class AddFiles extends SlingAllMethodsServlet {

   private static final long serialVersionUID = 2598426539166789515L;
   ResourceResolverFactory resourceFactory;
   ResourceResolver resourceResolver;

   protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)

   throws ServletException, IOException {

   boolean isMultipart = isMultipartContent(request);
   PrintWriter out = response.getWriter();
  if (isMultipart) {

  Map<String, RequestParameter[]> params = request.getRequestParameterMap();
  for (Map.Entry<String, RequestParameter[]> pairt : params.entrySet()) {

  String k = pairt.getKey();
   RequestParameter[] pArr = pairt.getValue();
   RequestParameter param = pArr[0];
   InputStream stream = param.getInputStream();
   out.println("The Sling Servlet placed the uploaded file here: " + writeToDam(stream, param.getFileName()));



   private ResourceResolverFactory resolverFactory;
  private String writeToDam(InputStream is, String fileName) {

   try {

  ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
   AssetManager assetMgr = resourceResolver.adaptTo(AssetManager.class);
   String newFile = "/content/dam/store/" + fileName ; assetMgr.createAsset(newFile, is,"image/jpeg", true);
  return newFile;

   } catch (LoginException e) {


   return null;



document.getElementById("exampleInputFile").addEventListener("change", function(){

   let nFiles = document.getElementById("exampleInputFile").files.length;
   document.getElementById("labelAddFiles").innerHTML = nFiles + " Archivos seleccionados";

// Crear la etiqueta <option> del select dependiendo de los juegos que esten
var xhttp = new XMLHttpRequest();"get", "/bin/gamer-store/tools/returnValues", true);

xhttp.onreadystatechange = function () {

   if (xhttp.readyState == 4 && xhttp.status == 200) {

   let data = JSON.parse(xhttp.responseText);

   for (let i = 0; i < data.length; i++) {

   let elementOption = document.createElement("option");
   let textElement = document.createTextNode(data[i].name);
   elementOption.setAttribute("value", data[i].name);



// --------------------------------------------------------------------------

// Evento al hacer click en el boton SAVE para enviar los datos
document.getElementById("btnAddFiles").addEventListener("click", function () {

   let filesForm = document.getElementById("exampleInputFile").files;
   let selectValue = document.getElementById("selectAddFiles").value;

   if (filesForm.length > 0 && selectValue != "null") {

   const formData = new FormData();

   formData.append("nameGame", selectValue);

   for (let i = 0; i < filesForm.length; i++) {

   var filesFormT = [];


  formData.append("imgFiles", filesFormT);
   filesFormT = [];"post", "/bin/gamer-store/tools/addFiles", true);


// -------------------------------------------------------------

document.getElementById("exampleInputFile").addEventListener("change", function () {

   let nodesChild = document.getElementById('imgAddFiles');

   while (nodesChild.hasChildNodes()) {


   let nFiles = document.getElementById("exampleInputFile").files.length;
   document.getElementById("labelAddFiles").innerHTML = nFiles + " Archivos seleccionados";


function readFile(input) {

   if (input.files && input.files[0]) {

   let fileU = document.getElementById('exampleInputFile');

   for (let i = 0; i < fileU.files.length; i++) {

   let reader = new FileReader();

   reader.onload = function (e) {

   var filePreview = document.createElement('img'); = 'file-preview';
   filePreview.src =;

   var previewZone = document.getElementById('imgAddFiles');





var fileUpload = document.getElementById('exampleInputFile');

fileUpload.onchange = function (e) {


I thank them for all the help they can provide



  Could you elaborate on your issue please ?

    what issue are you facing while trying to create folder ? Are you not able to create one ? Do you get any error in logs ?

                  Could you help us with the error you are getting in the error log to help you clearly with your issue ?



edubey 13-12-2018

I am not clear on you question,

All I am able to understand is you are trying to save multiple images in DAM but not where are you getting stuck and whats the problem you are seeing?