cancel
Showing results for 
Search instead for 
Did you mean: 

Upload large files to the repository and get its UUID

aelrwiny
Champ in-the-making
Champ in-the-making
I have large files to be uploaded to the repository, I used Alfresco UploadContentServlet (http://localhost:8080/alfresco/upload/workspace/SpacesStore/39d11b85-9fcb-4c58-9e84-75637eddb5b0/tes...) and the file is uploaded successfully but when i browse the path where i uploaded the file i couldn't find it although when hit Alfresco DownloadContentServlet (http://localhost:8080/alfresco/download/direct/workspace/SpacesStore/39d11b85-9fcb-4c58-9e84-75637ed...) by the browser the file is downloaded successfully.

The questions are:
1. Why i couldn't see the uploaded file in the repository?
2. Is there an way to get the UUID of the uploaded file?
3. Also i want to assign the uploaded file as a type of one of the types i defined in the custom content model which inherit cm:content, is there any way to do that?
4 REPLIES 4

kaynezhang
World-Class Innovator
World-Class Innovator
You can't sess the uploaded file in the repository is because you didn't upload the file correctly
If you uploaded the file correctly ,you definitely can see the file and can download it correctly.
After successfully upload the file ,you will get response body like following

{
   "nodeRef": "workspace://SpacesStore/0aebff95-cbeb-4b2c-adce-0d6ab90019d2",
   "fileName": "userguide.pdf",
   "status":
   {
      "code": 200,
      "name": "OK",
      "description": "File uploaded successfully"
   }
}
by parsing it you'll get node uuid.

You can specify content type by useing contentType parameter.

aelrwiny
Champ in-the-making
Champ in-the-making
Good Morning Kayne,

Thanks for for your reply. The following snippet of code is the method that I'm using for uploading the file what is the wrong and how to add the contentType parameter and its meta-data:

public static int uploadLargeFile(String folderUuid, File file, String mimeType, int bufSize) throws Exception {
      if (file == null || file.length() <= 0)
         throw new Exception("No file specified to be uploaded!");
      if (bufSize >= 0)
         bufSize = 1024;

      if (mimeType == null)
         mimeType = "";
      AuthenticationUtils.startSession("admin", "admin");
      String cmdUrl = "http://localhost:8080/alfresco/upload/workspace/SpacesStore/"
            + folderUuid
            + "/"
            + URLEncoder.encode(file.getName(), "UTF-8")
            + "?ticket=" + AuthenticationUtils.getTicket();      

      URL url;
      try {
         url = new URL(cmdUrl);
      } catch (MalformedURLException ex) {
         throw new Exception("Invalid upload url");
      }
      HttpURLConnection con = null;

      try {
         con = (HttpURLConnection) url.openConnection();
         log.info("HTTP PUT: " + url.toExternalForm());
         con.setRequestMethod("PUT");
      } catch (ProtocolException ex) {
         throw new Exception("Error connecting to upload url");
      } catch (IOException ex) {
         throw new Exception("Error connecting to upload url");
      }

      con.setDoOutput(true);
      con.setUseCaches(false);
      con.setAllowUserInteraction(false);
      con.setConnectTimeout(60000 * 10);
      con.setFixedLengthStreamingMode(file.length());

      BufferedInputStream inputStream = null;
      BufferedOutputStream outputStream = null;

      inputStream = new BufferedInputStream(new FileInputStream(file));

      outputStream = new BufferedOutputStream(con.getOutputStream());

      try {
         outputStream = new BufferedOutputStream(con.getOutputStream(), bufSize);
         int data;
         while ((data = inputStream.read()) != -1) {
            outputStream.write(data);
         }
      } catch (IOException ex) {
         throw new Exception("Error uploading file");
      } finally {
         outputStream.close();
         inputStream.close();
      }

      try {
         int retCode = con.getResponseCode();
         return retCode;
      } catch (IOException ex) {
         throw new Exception("Error uploading file");
      }
   }

kaynezhang
World-Class Innovator
World-Class Innovator
Following is sample code to call upload webscript

         String urlString = "http://localhost:8080/alfresco/service/api/upload?alf_ticket="
               + authTicket;
         HttpClient client = new HttpClient();

         PostMethod mPost = new PostMethod(urlString);
          File fileobj = new File("C:/userguide.pdf");
         Part[] parts = {
               new FilePart("filedata", filename, fileobj, filetype, null),
               new StringPart("filename", filename),
               new StringPart("description", description),
               // new StringPart("destination", destination),
               new StringPart("description", description),

               // modify this according to where you wanna put your content
               new StringPart("siteid", "kaynezhang"),
               new StringPart("containerid", "documentLibrary"),
         // new StringPart("uploaddirectory", "/Company Home")
         };
         mPost.setRequestEntity(new MultipartRequestEntity(parts, mPost
               .getParams()));
         int statusCode1 = client.executeMethod(mPost);
         System.out.println("statusLine>>>" + statusCode1 + "……"
               + "\n status line \n" + mPost.getStatusLine() + "\nbody \n"
               + mPost.getResponseBodyAsString());

gmasand09
Confirmed Champ
Confirmed Champ

Could you please tell like how you did it. i am doing the same thing but could able to do Smiley Sad