0

发出警报后,我如何检索已上传的文件并将它们发送到 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);
      }    
})
4

2 回答 2

0

请查看文档:

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可以检查的称为事件。

于 2018-02-22T18:44:29.560 回答
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);
          }
         })
于 2018-09-17T12:01:19.677 回答