You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

非企业版Alfresco文件上传实现求助(含Complete按钮触发逻辑)

Got it, let's tackle this file upload requirement in Alfresco where you want to push selected files to the server when clicking the Complete button—since you already have the TaskListener hooked up, here are concrete, working implementations for both Java and JavaScript:

Java Implementation

If you're using the Alfresco SDK and want full control over the upload logic, this approach leverages Alfresco's core services (NodeService, ContentService) to handle file creation and content writing.

  1. Create the TaskListener class
    This class will run when the task is completed, grab the uploaded file from the task properties, and save it to your target folder:
import org.alfresco.repo.task.TaskListener;
import org.alfresco.service.cmr.repository.*;
import org.alfresco.service.cmr.task.TaskDetails;
import org.alfresco.service.cmr.task.TaskService;
import org.springframework.beans.factory.annotation.Autowired;

public class FileUploadCompleteListener implements TaskListener {

    @Autowired
    private NodeService nodeService;

    @Autowired
    private ContentService contentService;

    @Autowired
    private TaskService taskService;

    @Override
    public void notify(TaskDetails taskDetails) {
        // Pull file details from task form properties
        String fileName = (String) taskDetails.getProperties().get("uploaded_file_name");
        byte[] fileContent = (byte[]) taskDetails.getProperties().get("uploaded_file_content");

        if (fileName != null && fileContent != null) {
            // Get your target folder (customize this to point to your desired repo location)
            NodeRef targetFolder = getTargetUploadFolder();

            // Create a new content node in the target folder
            NodeRef newFileNode = nodeService.createNode(
                targetFolder,
                ContentModel.ASSOC_CONTAINS,
                QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, fileName),
                ContentModel.TYPE_CONTENT
            ).getChildRef();

            // Set basic metadata
            nodeService.setProperty(newFileNode, ContentModel.PROP_NAME, fileName);
            nodeService.setProperty(newFileNode, ContentModel.PROP_TITLE, fileName);

            // Write the file content to the node
            ContentWriter contentWriter = contentService.getWriter(newFileNode, ContentModel.PROP_CONTENT, true);
            contentWriter.setMimetype("application/octet-stream"); // Update this based on your file type
            contentWriter.setEncoding("UTF-8");
            contentWriter.putContent(fileContent);

            // Optional: Attach the file to the task for reference
            taskService.addTaskAttachment(taskDetails.getId(), newFileNode);
        }
    }

    // Helper method to retrieve your target folder NodeRef
    private NodeRef getTargetUploadFolder() {
        // Example: Return a specific folder by Node ID (replace with your logic)
        // You can use FileFolderService to look up paths dynamically too
        return new NodeRef("workspace://SpacesStore/your-target-folder-node-id");
    }
}
  1. Register the listener as a Spring bean
    Add this to your project's Spring context file (e.g., context.xml):
<bean id="fileUploadCompleteListener" class="com.yourorg.alfresco.listeners.FileUploadCompleteListener">
    <property name="nodeService" ref="NodeService"/>
    <property name="contentService" ref="ContentService"/>
    <property name="taskService" ref="TaskService"/>
</bean>
  1. Link the listener to your task in BPMN
    Update your user task definition to trigger the listener on completion:
<userTask id="fileUploadTask" name="Upload Your File">
    <extensionElements>
        <activiti:taskListener event="complete" delegateExpression="${fileUploadCompleteListener}"/>
    </extensionElements>
    <!-- Your form properties here, including the file upload field mapped to uploaded_file_name and uploaded_file_content -->
</userTask>
JavaScript Implementation

If you want a no-compile, script-based solution (great for quick tweaks or non-SDK projects), use an Alfresco JavaScript Task Listener.

  1. Create the script file
    Save this as file-upload-complete.js in alfresco/extension/scripts (or your AMP's config/alfresco/extension/scripts folder):
function notify(task) {
    // Get uploaded file details from task properties
    var fileName = task.properties["uploaded_file_name"];
    var fileContent = task.properties["uploaded_file_content"];

    if (fileName && fileContent) {
        // Define target folder (example: Company Home > Uploads)
        var targetFolder = companyhome.childByNamePath("Uploads");
        
        // Create folder if it doesn't exist
        if (!targetFolder) {
            targetFolder = companyhome.createFolder("Uploads");
        }

        // Create the file node
        var newFile = targetFolder.createFile(fileName);
        
        // Write content and set metadata
        newFile.content = fileContent;
        newFile.properties["cm:title"] = fileName;
        newFile.save();

        // Optional: Attach the file to the task
        task.addAttachment(newFile);
    }
}
  1. Attach the script to your task in BPMN
    Update your user task to use the script listener:
<userTask id="fileUploadTask" name="Upload Your File">
    <extensionElements>
        <activiti:taskListener event="complete" scriptFormat="javascript">
            <activiti:script>resource://alfresco/extension/scripts/file-upload-complete.js</activiti:script>
        </activiti:taskListener>
    </extensionElements>
    <!-- Form definition with file upload field mapped to the required properties -->
</userTask>

Key Notes

  • Form Field Mapping: Make sure your task form has a file upload field that maps to uploaded_file_name (stores the filename) and uploaded_file_content (stores the file byte array). In Alfresco Share, you'd use a <field id="uploaded_file" type="fileupload"> with appropriate property mappings.
  • Permissions: Ensure the user completing the task has Write permissions on the target folder in Alfresco.
  • MIME Types: For Java, adjust the mimetype to match your file type (e.g., application/pdf for PDFs). In JavaScript, Alfresco may auto-detect it, but you can set it explicitly with newFile.mimetype = "image/png"; if needed.

内容的提问来源于stack exchange,提问作者rohit_mhetre

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:39:54