繁体   English   中英

如何使用lightning:fileUpload将上传的文件发送到Apex-Salesforce Lightning

[英]How to Send the uploaded file to Apex using lightning:fileUpload - Salesforce lightning

Alert之后,我如何检索上载的文件并将其发送到Apex类?

同样在APEX类上,我们用于接收发送文件的输入参数类型是什么?

组件代码

<lightning:fileUpload label="Upload Multiple files" 
                               multiple="false" 
                              accept=".pdf, .png, .jpg"
                              recordId="{!v.recordId}"
                              aura:id="multipleUpload"
                             onuploadfinished="{!c.handleUploadFinished}" />

JS控制器

({
    handleUploadFinished: function (component, event, helper) {
    // Get the list of uploaded files
    var uploadedFiles = event.getParam("files");
        alert("Files uploaded length  : " + uploadedFiles.length);
      }    
})

请查看文档:

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/aura_compref_lightning_fileUpload.htm

闪电文件上传组件,上传文件并将其附加到记录。

您可以使用以下属性指定记录来附加文件:

recordId => String => The record Id of the record that the uploaded file is associated to.

如果要验证文件或对它们执行一些逻辑,请使用下面提供的回调函数:

onuploadfinished => Action => The action triggered when files have finished uploading.

该文档显示了此回调函数示例:

({
    handleUploadFinished: function (cmp, event) {
        // Get the list of uploaded files
        var uploadedFiles = event.getParam("files");
        alert("Files uploaded : " + uploadedFiles.length);
    }
})

如您所见,该函数收到一个事件,称为事件,可以检查该files

在发送docId的状态下,您可以使用JSON.stringify(uploadedFiles [0])以字符串形式发送文件

    ({
     handleUploadFinished: function (component, event, helper) {
     var uploadedFiles = event.getParam("files");
     var action = cmp.get("c.saveDoc");
     action.setParams({
          parentId : cmp.get("v.myRecordId"),
          contentDocId : uploadedFiles[0].documentId
      });


      action.setCallback(this, function(response) {
         var state = response.getState();
         if (state === "SUCCESS") {

            var toastEvent = $A.get("e.force:showToast");
            toastEvent.setParams({
                "title": "Success!",
                "message": "File "+uploadedFiles[0].name+" Uploaded successfully."
            });
            toastEvent.fire();
            var cmpEvent = cmp.getEvent("cmpEvent");
            cmpEvent.fire();
          }
          else {
            console.log("Fail");
          }

         });
         $A.enqueueAction(action);
          }
         })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM