如何使用node+jquery ajax实现图片上传?


如果使用form表单上传文件的话会造成网页的刷新,所以不想用这种方法,直接用ajax传送要如何实现,node后端要如何接收?

jquery jquery-ajax node.js

迷糊D蜗牛 9 years, 10 months ago

http://segmentfault.com/a/1190000002548247


 <form id="form1" enctype="multipart/form-data" method="post" action="/session/sendImg?subID=3&subsID=2&ssID=163">
    <input type="file" name="file" id="file"/>
    <input type="text" name="name" id="name"/>
    <input type="button" onclick="sendForm()" value="Upload" />
</form>


 function sendForm(){
    var fd = new FormData();
    fd.append("file",$('#file')[0].files[0]);
    fd.append("name",$('#name').val());
    $.ajax({
        type:'POST',
        dataType:'text',
        processData: false,  // 告诉JSLite不要去处理发送的数据
        contentType: false,   // 告诉JSLite不要去设置Content-Type请求头
        data:fd,
        url:'/session/sendImg?subID=3&subsID=2&ssID=163',
        success:function(data){
           console.log('success:',data)
        },
        error:function(d){
           console.log('error:',d)
        }
    })
}

node接收你就搜一下,怎么玩儿的。

kazama answered 9 years, 10 months ago

前端:
使用jquery.form.js的ajaxform异步提交表单

后端(node):
使用node gm模块(图片压缩剪切处理)
https://github.com/aheckmann/gm

使用node-formidable获取表单提交内容及上传的图片
https://github.com/felixge/node-formidable

Melody. answered 9 years, 10 months ago

Your Answer