码迷,mamicode.com
首页 > Web开发 > 详细

Ajax 之【文件上传】

时间:2015-03-19 16:10:54      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:


// 前台

var formData = new FormData(); var file = document.getElementById(‘myFile‘).files[0]; formData.append(‘myFile‘, file); var xhr = new XMLHttpRequest(); xhr.open(‘post‘, ‘/upload‘, true); xhr.upload.onprogress = function(e) { if (e.lengthComputable) { var percentage = (e.loaded / e.total) * 100; $(‘div.progress div.bar‘).css(‘width‘, percentage + ‘%‘); } }; xhr.onerror = function(e) { showInfo(‘An error occurred while submitting the form. Maybe your file is too big‘); }; xhr.onload = function() { showInfo(this.statusText); $(‘#message‘).val(window.location.origin + "/" + xhr.responseText.replace("\\","/")); }; xhr.send(formData);

 

//后端
var express = require(‘express‘),
 app.use(express.bodyParser({ 
    keepExtensions: true, 
    uploadDir: __dirname + web_upload_directory,
    limit: 1024MB
  }));

app.post(‘/‘, function(req, res) {
    colog.info(Timestamp() + "File uploaded: " + req.files.myFile.path);
    var filepath = req.files.myFile.path;
    res.send(filepath.replace(web_upload_rootpath,""));
  res.end();
});

  

Ajax 之【文件上传】

标签:

原文地址:http://www.cnblogs.com/seasonxin/p/4350641.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!