如何使用Javascript远程处理将附件上传到Salesforce?

问题描述

调用RemoteAction之前,我将其打印在控制台上并确认已存储附件的参数。 但是,当我输出控制器的调试日志时,我可以得到fileName的值,但是fileList为NULL。

我希望控制器接收存储在fileList中的参数。我该怎么办?

我有以下Visualforce页面,并以apex类作为控制器:

$("#uploadButton").click(function(){
    var uploadFile = document.getElementById('uploadFile');
    var param = new Object();
    param.fileName = '';
    param.fileList = [];
    var returnParam = new Object();

    for(let i = 0; i < uploadFile.files.length; i++) {
        var file = uploadFile.files[i];
        if(i == 0) {
            param.fileName = file.webkitRelativePath;
        } else {
            returnParam = readFile(file);
            param.fileList.push(returnParam);
        }
    }
    uploadAttachments(param);
});

function readFile(file) {
    var inputFile = new Object();
    var reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onloadend = function() {
        var dataURL = reader.result;
        inputFile['name'] = file.name;
        inputFile['file'] = dataURL.match(/,(.*)$/)[1];
    };
    return inputFile;
}

function uploadAttachments(param) {
    console.log(param);
    Visualforce.remoting.Manager.invokeAction(
        "{!$RemoteAction.UploadController.uploadAttachments}",param,function(result,event) {
            if(event.status) {
                if(result == null) {
                    alert('An unexpected error has occurred');
                }
            }
        }
    );
}
public with sharing class UploadController {
    public UploadController() {    
    }

    @RemoteAction
    public static Map<String,Object> uploadAttachments(FileUploadWrapper file) {
        Map<String,Object> remoteActionResult = new Map<String,Object>();
        System.debug(file); // this prints the empty List. 
        return remoteActionResult;
    }
}

在顶点类中用作控制器的包装器类:

public with sharing class FileUploadWrapper {
    public String fileName {get; set;}
    public List<FileInfo> fileList {get; set;}

    public FileUploadWrapper() {
        this.fileName = '';
        this.fileList = new List<FileInfo>();
    }

    public class FileInfo {
        public String name {get; set;}
        public Blob file {get; set;}
        
        public FileInfo() {
            this.name = '';
            this.file = null;
        }
    }
}

如果有人可以帮助,请告诉我。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)