简体   繁体   English

将 HTML 页面中的图像保存到 NetSuite 文件柜抛出 UNEXPECTED_ERROR

[英]Saving Image From HTML page To The NetSuite File Cabinet throw UNEXPECTED_ERROR

I have a suitelet that creates a html page.我有一个创建 html 页面的套件。 This page has a html element input type file.此页面有一个 html 元素输入类型文件。 I am trying to take that file and upload it to the file cabinet.我正在尝试获取该文件并将其上传到文件柜。 This is not done on a NetSuite form so the file element is not a netsuite file object.这不是在 NetSuite 表单上完成的,因此文件元素不是 netsuite 文件对象。

The javascript on the HTML page is as follows HTML页面上的javascript如下

function uploadPhotoToNetSuite(){
var bookingid = $("#txtAddPhotoBookingId").val();
var caseid = $("#txtAddPhotoCaseId").val();
var isCase = caseid != "";

var base64Image = document.getElementById("imageToAdd").src;
var formData = new FormData();

    formData.append("operations", 'uploadphoto');
    formData.append("bookingid", bookingid);
    formData.append("caseid", caseid);
    formData.append("image", base64Image);    

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
    if (this.readyState == 4) {
        var objResponse = JSON.parse(xhr.responseText);
        if(!objResponse.uploadphoto.success){
            alert(objResponse.uploadphoto.err);
        } else {
            closeLoading();
        }
        clearPhotoUpload();
    }
};

xhr.open("POST", stAPIURL, true);
loading("Uploading Photo");
xhr.send(formData);
}

Then this matches to a method in my suitelet as follows.然后这与我的套件中的方法相匹配,如下所示。

function uploadPhoto(params, recUser){
    try{
        var imageFolder = 767406;
        var thePhoto = params.image;
        var filetype = "png";
        if(thePhoto.indexOf("image/png") > -1) filetype = "png";
        var theFile = file.create({
            name: 'test.' + filetype,
            fileType: filetype == "jpg" ? file.Type.JPGIMAGE : file.Type.PNGIMAGE,
            contents: thePhoto,
            description: 'This is a plain text file.',
            encoding: file.Encoding.UTF8,
            folder: imageFolder,
            isOnline: true
        });
        var id = theFile.save();
    } catch(err){
        return {
            success : false,
            err : JSON.stringify(err)
        }
    }
    return {
        success : true
    }
}

When this is happens I am getting the error UNEXPECTED_ERROR.发生这种情况时,我收到错误 UNEXPECTED_ERROR。 The variable thePhoto is a base64 string of the image.变量 thePhoto 是图像的 base64 字符串。

UPDATE:更新:

I change the suitelet code to create a text file and the file uploaded perfectly and the base64 string was in the text file.我更改了suitelet 代码以创建一个文本文件,并且文件上传完美,并且base64 字符串在文本文件中。 When I took that base64 string and put it through a convertor, the image I uploaded was the result.当我将那个 base64 字符串放入转换器时,我上传的图像就是结果。

With this in mind, I changed the code again to;考虑到这一点,我再次将代码更改为;

var theFile = file.create({
    name: 'test.jpg',
    fileType: file.Type.JPGIMAGE,
    contents: thePhoto,
    description: 'This is a plain text file.',
    encoding: file.Encoding.UTF8,
    folder: imageFolder,
    isOnline: true
});

And uploaded a .jpg file.并上传了一个 .jpg 文件。 Once again I got the error.我又一次得到了错误。

I was experiencing the same issue and finally figured out the resolution.我遇到了同样的问题,终于找到了解决方案。 NetSuite does convert Base64 image data to a JPEG file in the file cabinet automatically, but it can only be the raw base64 data. NetSuite 确实会自动将 Base64 图像数据转换为文件柜中的 JPEG 文件,但只能是原始 base64 数据。 The base64 metadata at the beginning needs to be removed.需要去掉开头的base64元数据。 After several hours of frustration, adding the first two lines to the function below allowed it to save properly as a JPEG file (without the unexpected error).经过几个小时的挫折后,将前两行添加到下面的函数中,可以将其正确保存为 JPEG 文件(没有出现意外错误)。

    function saveJPEGFile(fileName, fileContents){
            if (fileContents.startsWith('data:image/jpeg;base64'))
              fileContents=fileContents.substring('data:image/jpeg;base64,'.length);
               
            log.debug('saving file:',`${fileName} : ${fileContents.length} : ${fileContents}`)
            var fileObj = file.create({
                name : fileName,
                fileType: file.Type.JPGIMAGE,
                contents: fileContents,
                folder :  1127
            });
            var fileId = fileObj.save();
               
        }

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

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