简体   繁体   English

附加base64编码的文件nodejs

[英]Attaching base64 encoded file nodejs

I am trying to send a soap request with an attachment. 我正在尝试发送带有附件的肥皂请求。 Everything works fine except that the attachment i send is always of zero bytes. 一切正常,除了我发送的附件始终为零字节。 The soap server accepts a Base64 encoded file and i had achieved to do it in Java using the code soap服务器接受Base64编码的文件,而我已经使用代码在Java中实现了该功能

OutputStream outputStream = new ByteArrayOutputStream()
outputStream.writeTo(fileOutputStream);
Base64.encode(outputStream.toByteArray())//argument passed to the function which sends this to the SOAP API

I want to replicate the same with node but i am unable to do so. 我想与节点复制相同,但我不能这样做。 Below is the function i am using to achieve this. 以下是我用来实现此目的的功能。 I am reading some files from the client and trying to send it to the SOAP API. 我正在从客户端读取一些文件,并尝试将其发送到SOAP API。 I have marked the place in the code responsible to read and append the data the rest is just for reference. 我已在代码中标记了负责读取和附加数据的位置,其余仅作为参考。

function createSoapEntryWithAtt(req,response){
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        let filesArr = []
        for(objkeys in files){
            filesArr.push(files[objkeys])
        }
        return Promise.all(filesArr.map(item => {
            return new Promise((res,rej) => {
                var oldpath = item.path;
                var newpath = 'C:/user/' + item.name;
                **var data = fs.readFileSync(oldpath).toString('base64');
                let result = []
                for (var i = 0; i < data.length; i += 2)// trying to create a 64bit byte array
                    result.push('0x' + data[i] + '' + data[i + 1])**

                console.log(result)
                if(data)
                    res({ [`${item.name}`]: result }) 
                rej("Error occured")
            })

        })).then(data => {
            let url = config.url
            var credentials = {
                AuthenticationInfo: {
                    userName: "user",
                    password: "passwd"
                }
            }
            let args = {
                Notes: "Testing From Node App",
            }
            let count = 0
            for (index in data) {
                if (count <= 3) {
                    **for(keys in data[index]){
                        //console.log(data[index][keys])
                        args[`Attachment${++count}_Name`] = keys
                       args[`Attachment${++count}_Data`] = data[index][keys]//Attaching the file read  
                    }
                }**
            }
            soap.createClient(url, function (err, client) {
                client.addSoapHeader(credentials)

                client.CreateWorkInfo(args, function (err, res) {
                    if (err) {
                        console.log("Error is ----->" + err)
                    } else {
                        console.log("Response is -----> " + res)
                        response.end();
                    }
                })
            })

        })

    });

}

Please ignore this question .... and thanks and sorry if anyone wasted time on this question. 请忽略此问题....,如果有人在此问题上浪费了时间,请多谢。 The error was a careless mistake from my side in the line args["Attachment${++count}_Name"] = keys args["Attachment${++count}_Data"] = data[index][keys] . 该错误是args["Attachment${++count}_Name"] = keys args["Attachment${++count}_Data"] = data[index][keys] Here as i am incrementing the count in both lines there is a mismatch in the sense that Attachment name will be 1 and then in the second line Attachment data will be 02 and hence the name does not contain any data. 在这里,当我在两行中增加计数时,在某种意义上不匹配,即附件名称将为1,然后在第二行中附件数据将为02,因此该名称不包含任何数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM