如何使用lightning发送上传的文件到Apex:fileUpload - Salesforce lightning
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用lightning发送上传的文件到Apex:fileUpload - 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);
}
})
答案
请查看文档:
闪电文件上传组件,上传文件并将其附加到记录。
您使用以下属性指定要将文件附加到的记录:
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的instate你可以使用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);
}
})
以上是关于如何使用lightning发送上传的文件到Apex:fileUpload - Salesforce lightning的主要内容,如果未能解决你的问题,请参考以下文章
如何在 APEX 应用程序的 csv 文件的批量数据插入中使用 forall 语句
将 Excel 文件传输到 APEX 中的数据库服务器目录路径