【问题标题】:How to Send the uploaded file to Apex using lightning:fileUpload - Salesforce lightning如何使用闪电将上传的文件发送到 Apex:fileUpload - Salesforce 闪电
【发布时间】:2018-02-21 02:54:16
【问题描述】:

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

【问题讨论】:

    标签: salesforce salesforce-lightning


    【解决方案1】:

    请查看文档:

    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 的事件,可以对其进行检查。

    【讨论】:

    • Hi Tiaan,感谢您的回复,我尝试调用“handleUploadFinished”回调函数,但无法访问文件,例如我想知道如何计算文件的大小我已经上传了
    • 你好@Raghu,请使用传递的文件属性,循环属性(它是一个数组)并检查大小。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 2018-04-16
      相关资源
      最近更新 更多