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

图片和文件上传

时间:2016-12-03 15:44:18      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:.com   his   blog   length   this   src   creat   tor   doc   

用于记录以供下次快速查阅

$(‘#fileImage‘).change(function(e){
    e.stopPropagation();
    e.preventDefault();    
    var files=this.files;
    var imgSrc;
    if(files.length){
        if(files[0].type.indexOf("image") == 0) {
            if (files[0].size >= 512000) {
                alert(‘图片"‘+ files[0].name +‘"大于500k,请重新选择!‘);    
                return;
            }
        } else {
            alert(‘文件"‘ + files[0].name + ‘"不是图片。‘);
            return;
        }
          imgSrc=window.URL.createObjectURL(files[0]);
          $(‘#user-avatar-img‘).attr(‘src‘,imgSrc);
    }
});
$(‘#submitBtn‘).on(‘click‘,function(){
    var data=new FormData(document.forms.nameItem(‘uploadForm‘));
//data.append(‘file‘,file); //添加或指定传递的对象
var req=new XMLHttpRequest(); req.open(‘POST‘,‘http://url‘,true); req.send(data); })

FormData参考自:http://www.cnblogs.com/lhb25/p/html5-formdata-tutorials.html

files对象:https://developer.mozilla.org/zh-CN/docs/Using_files_from_web_applications

 

图片和文件上传

标签:.com   his   blog   length   this   src   creat   tor   doc   

原文地址:http://www.cnblogs.com/zhenxianluo/p/6128542.html

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