Expand my Community achievements bar.

Create Folder DAM y send multiple images

Avatar

Level 2

1646186_pastedImage_1.png

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

https://helpx.adobe.com/experience-manager/using/upload_experience_manager_dam.html

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

4 Replies

Avatar

Level 10

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?

Avatar

Community Advisor

Kevin

  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 ?

Thanks

Veena

Avatar

Level 2

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

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

public class AddFiles extends SlingAllMethodsServlet {

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

   @Override
   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()));
   }

  }

  }

   @Reference
   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) {

  e.printStackTrace();
   }

   return null;
   }

}

ajax

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();
xhttp.open("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.appendChild(textElement);
   elementOption.setAttribute("value", data[i].name);
   document.getElementById("selectAddFiles").appendChild(elementOption);
   }

  }

}

xhttp.send(null);
// --------------------------------------------------------------------------

// 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 = [];
   filesFormT.push(filesForm[i]);

   }

  formData.append("imgFiles", filesFormT);
   filesFormT = [];

   xhttp.open("post", "/bin/gamer-store/tools/addFiles", true);
   xhttp.send(formData);

   }

});
// -------------------------------------------------------------



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

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

   while (nodesChild.hasChildNodes()) {

  nodesChild.removeChild(nodesChild.firstChild);
   }

   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');
   filePreview.id = 'file-preview';
   filePreview.src = e.target.result;

   var previewZone = document.getElementById('imgAddFiles');
   previewZone.appendChild(filePreview);
   }

  reader.readAsDataURL(input.files[i]);

   }

  }

}

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

fileUpload.onchange = function (e) {

   readFile(e.srcElement);
}

I thank them for all the help they can provide

Avatar

Community Advisor

Kevin,

   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

Thanks

Veena