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

时间:2018-02-21 02:54:17

标签: salesforce salesforce-lightning

警报之后如何检索上传的文件并将其发送到Apex类?

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

组件代码

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

JScontroller

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

2 个答案:

答案 0 :(得分:0)

请查看文档:

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

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

使用以下属性指定要附加文件的记录:

recordId =&gt; String =&gt; The record Id of the record that the uploaded file is associated to.

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

onuploadfinished =&gt; Action =&gt; 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的事件,可以进行检查。

答案 1 :(得分:0)

  

在发送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);
          }
         })