简体   繁体   English

如何通过Ajax从前端向后端发布图像信息

[英]How to post a image information from front end to back end via ajax

Front end js: 前端js:

        var newUser = {
        'username': $('#addUser fieldset input#inputUserName').val(),
        'email': $('#addUser fieldset input#inputUserEmail').val(),
        'fullname': $('#addUser fieldset input#inputUserFullname').val(),
        'age': $('#addUser fieldset input#inputUserAge').val(),
        'sex': $('input:radio[name="sex"]:checked').val(),
        'profileimage':$('input[name="profileimage"]').val()
    }
    console.log($('input[name="sex"]').val());
    // use AJAX to post object to adduser service
    $.ajax({
        type: 'POST',
        data: newUser,
        url: '/users/adduser',
        dataType: 'JSON'
    }).done(function (response) {...

Back End js(use express.js and multer) 后端js(使用express.js和multer)

router.post('/adduser', upload.single('profileimage'), function (req, res) {
var db = req.db;
var collection = db.get('userlist');

var username = req.body.username;
var email = req.body.email;
var age = req.body.age;
var sex = req.body.sex;
var fullname = req.body.fullname;

if (req.file) {
    var profileimage = req.file.filename;
} else {
    var profileimage = 'noimage.jpg';
}

collection.insert({
    "username": username,
    "email": email,
    "age": age,
    "sex": sex,
    "fullname": fullname,
    "profileimage": profileimage
}, function (err, result)...

The code above is not work. 上面的代码不起作用。 And what should I do insert the image information to my mongoDB database, and could display it as well. 而且我应该怎么做将图像信息插入到我的mongoDB数据库中,并可以显示它。

I believe you're looking for the File Reader in Javascript. 我相信您正在寻找Javascript中的File Reader Here is some excellent documentation on how to use the readAsDataURL which will allow you to interact with a byte array of the file and submit it into a database as a blob or byte array. 是一些有关如何使用readAsDataURL的出色文档,该文档使您可以与文件的字节数组进行交互,并将其作为Blob或字节数组提交到数据库中。

Formdata could help to do this, problem solved. 问题解决了,Formdata可以帮助实现这一点。

        var newUser = new FormData();
    newUser.append('username', $('#addUser fieldset input#inputUserName').val());
    newUser.append('email', $('#addUser fieldset input#inputUserEmail').val());
    newUser.append('fullname', $('#addUser fieldset input#inputUserFullname').val());
    newUser.append('age', $('#addUser fieldset input#inputUserAge').val());
    newUser.append('sex', $('input:radio[name="sex"]:checked').val());
    newUser.append('profileimage', $('#uploadImage')[0].files[0]);

    console.log($('input:radio[name="sex"]:checked').val());
    //console.log(newUser);
    // use AJAX to post object to adduser service
    $.ajax({
        type: 'POST',
        data: newUser,
        url: '/users/adduser',
        dataType: 'JSON',
        contentType: false,
        processData: false
    }).done(function (response) {

暂无
暂无

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

相关问题 如何通过Ajax在Wordpress前端上载图像 - How to upload image on wordpress front-end via ajax NodeJS(后端)从POST调用接收数据,然后如何在ReactJS(前端)中显示? - NodeJS (back-end) receive data from POST call then how to show in ReactJS (front-end)? 从 Laravel 后端向 React 前端发送信息,而不先从前端发出请求 - Sending information from Laravel back end to React front end without making request from front first 如何使用 WebSocket 从前端 Javascript 发送图像到后端 Flask? - How to send and image from Javascript front-end to Flask back-end with WebSocket? 如何使用javascript或angularjs将文件从前端发送到后端 - how to send a file from front end to back end with javascript or angularjs 从后端到前端返回数组 - Return Array from Back End to Front End 如何通过AJAX调用将内容类型为“ image / jpeg”的画布图像从GUI保存到后端? - How to save a canvas image from GUI to back-end via AJAX call with content-type “image/jpeg”? 如何从反应前端向节点后端发出发布请求? 获得 404 - How do I make a post request from a react front-end to a node back-end? Getting 404 如何在wordpress的前端使用ajax保存帖子和自定义字段值? - How to save a post and custom fields values with ajax in front end in wordpress? 将数据从 javascript 前端发送到 Flask 后端而不使用 Ajax 的方式 - Way to send data from javascript front-end to Flask back-end WITHOUT Ajax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM