首页 > Web开发 > 详细

Ajax 之【文件上传】

时间:2015-03-19 16:10:41      阅读:292      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!