首页 > 解决方案 > 使用 jquery 发送 base64 编码图像列表

问题描述

当我将 ajax 请求发送到 node.js 服务器时,包含我的图像列表的参数不会出现。(我在服务器上将请求修改为 50mb)这是我的 ajax 请求:

$.ajax({
                url: '/post_vehicle',
                type: "POST",
                traditional: true,
                data: {
                    images: list,   // here is the problem, this var does not arrive on the server
                    brand: $('.post_brand :selected').text(),
                    model: $('.post_mark :selected').text(),
                    location: $('.post_location :selected').text(),
                    year: $('.post_year :selected').text(),
                    fuel_type: $('post_fuel_type :selected').text(),
                }
            })

我在 ajax 之前使用了 console.log 来检查列表是否有图像,它会向我显示所有图像。

这是ajax中列表的值: 列表值

标签: jquerynode.jsajaxxmlhttprequest

解决方案


我放弃了将图像保存为 base64,而是像这样使用 FormData:

let formData = new FormData($('#sell_form')[0]); 
// #sell_form is the form id where i have all the data that i need.
//Now formData variable will contain all the values from your form inputs

Ajax 请求

$.ajax({
            url: '/post_vehicle',
            type: 'POST',
            data: formData,
            dataType:'json',
            processData: false,
            contentType: false,
        });

如果你想添加额外的数据

formData.append("Key",'Value');

推荐阅读