zoukankan      html  css  js  c++  java
  • 图片上传

            $('input').change(function(){
                var fileObj = this.files[0];
    
                //检查是否支持FileReader对象
                if (typeof FileReader!= 'undefined'){
                    var acceptTypes = /.(jpe?g|png)$/i;
    
                    if ( !(acceptTypes.test(fileObj.name))){
                        alert('请上传jpg,png格式的文件');
                        return;
                    }
    
                    if (fileObj.size>1*1024*1024){
                        alert('上传图片不能大于1M');
                        return;
                    }
    
                    var reader = new FileReader();
                    reader.onload = function(){
                        var $img = $('<img>');
                        $img.attr('src',this.result);  //这只是在本地预览
                        $img.appendTo($('body'));
                    }
    
                    reader.readAsDataURL(fileObj);  //文件转成base64

             //上传到服务器

             var formData = new FormData();
             formData.append('file',fileObj);
             $.ajax({
               type:'POST',
               url:'',
               data:formData,
               processData : false, //告诉jquery不要去处理发送的数据
               contentType: false, //告诉jquery不要去设置content-type请求头
             }).done(function(){}).fail(function(){});
    }

    参考:http://www.jianshu.com/p/46e6e03a0d53

  • 相关阅读:
    bzoj1257
    bzoj1833
    bzoj3505
    bzoj2226
    bzoj1263
    bzoj2429
    bzoj1854
    bzoj3555
    bzoj1877
    放两个模版
  • 原文地址:https://www.cnblogs.com/joya0411/p/5182444.html
Copyright © 2011-2022 走看看