单图上传

<div class="imgUp">
     <label>头像单图</label>
     <input type="file" name='photo' class="mui-input-clear" id='upimg' style="opacity: 0;" >
      <img src="data:images/up.png" alt="" id='imgContent'>
</div>

js

$('#upimg').on('change',function(){                                                 //选中图片后展示在页面
    var filePath = $(this)[0].files[0].name //获取到input的value,里面是文件的路径
   //console.log(filePath) //1.png
   fileFormat = filePath.split('.')[1].toLowerCase()
   //console.log(fileFormat) //png
   src = window.URL.createObjectURL(this.files[0]) //转成可以在本地预览的格式
   //console.log(src) //blob:null/11ea5a2d-7270-4035-b5c4-4e50c5c061e7

// 检查是否是图片
  if( !fileFormat.match(/png|jpg|jpeg/) ) {
      alert('上传错误,文件格式必须为:png/jpg/jpeg')
     return
  }

$('#imgContent').attr('src',src)

})

多图上传

<div class="imgUp1">
    <label>详情页多图</label>
    <input type="file" class="mui-input-clear" id="upimg1" multiple style="opacity: 0;">
    <img src="data:images/up.png" alt="" id='imgContent1'><br>
    <div id="previewImg">
           
    </div>
</div>

js

var fileall = []  //存放图片的容器

$('#upimg1').on('change', function(){
     var imgFiles = $(this)[0].files
     for (i=0;i<imgFiles.length;i++){
              filePath = imgFiles[i].name
              fileFormat = filePath.split('.')[1].toLowerCase()
              src = window.URL.createObjectURL(imgFiles[i])
              if( !fileFormat.match(/png|jpg|jpeg/) ) {
                    alert('上传错误,文件格式必须为:png/jpg/jpeg')
                    return
              }
             var preview = document.getElementById("previewImg")
             var img = document.createElement('img')
             img.width = 120
             img.src = src
             preview.prepend(img)

fileall.push($("#upimg1")[0].files[0])
            console.log(fileall)

})

点击提交或者上传时

var formData1 = new FormData();

$("#submit").click(function(){

for(var i = 0;i<fileall.length;i++){
         formData1.append("img",fileall[i]);
    }

formData1.append("photo", $("#upimg")[0].files[0]);

$.ajax({
        url: comPageUrl+'/applyVote',
        type: 'POST',
        async:true,
        xhrFields:{
            withCredentials:true
         },
        contentType:false,
        processData:false,     //这个很有必要,不然不行
        dataType:"json",
        mimeType:"multipart/form-data",
        data: formData1,
        success: function(res){
                  console.log(res)
                 if(res.status==200){
                     alert("上传成功")
                 }

},
        error: function(){
               alert('服务器发生错误!');
        }
    })

})

js formData图片上传(单图上传、多图上传)后台java的更多相关文章

  1. 怎么样通过php使用html5实现多文件上传?(php多图上传)

    <!DOCTYPE html><html lang="zh-cn"> <head> <meta charset="utf-8&q ...

  2. js文件/图片从电脑里面拖拽到浏览器上传文件/图片

    1.效果展示 2.html 代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <!DOCTYPE html> <html lang=& ...

  3. CAD控件:梦想CAD控件功能更新 清除图上的所有高亮实体

    1,修正得组里面的实体,把删除实体也返回的错误   2,修正代理实体改不了颜色问题.   3,修正捕捉块插入点,有时会跑到很远的位置问题.   4.MxDrawChange类增加ToBlockRefe ...

  4. ASP.NET MVC中使用Dropzone.js实现图片的批量拖拽上传

    说在前面 最近在做一个MVC相册的网站(这里),需要批量上传照片功能,所以就在网上搜相关的插件,偶然机会发现Dropzone.js,试用了一下完全符合我的要求,而且样式挺满意的,于是就在我的项目中使用 ...

  5. js实现图片上传预览及进度条

    原文js实现图片上传预览及进度条 最近在做图片上传的时候,由于产品设计的比较fashion,上网找了比较久还没有现成的,因此自己做了一个,实现的功能如下: 1:去除浏览器<input type= ...

  6. html + js 实现图片上传,压缩,预览及图片压缩后得到Blob对象继续上传问题

    先上效果 上传图片后(设置了最多上传3张图片,三张后上传按钮消失) 点击图片放大,可以使用删除和旋转按钮 (旋转功能主要是因为ios手机拍照后上传会有写图片被自动旋转,通过旋转功能可以调正) html ...

  7. thinkphp 多图片上传 单图片上传

    不管是单图片上传还是多图片上传都必须要引用这两个js 下载地址 链接:http://pan.baidu.com/s/1eStkUt0 密码:asvo <script src="Publ ...

  8. js实现图片粘贴上传到服务器并展示

    最近看了一些有关于js实现图片粘贴上传的demo,实现如下: (这里只能检测到截图粘贴和图片右键复制之后粘贴) demo1: document.addEventListener('paste', fu ...

  9. 原生js实现图片预览并上传

    最近主导的PC客户端网站重构工程告一段落,下一阶段开始给公司APP开发H5页面,技术栈是react.最近碰到一个需求:需要在H5页面上添加身份证照片,预览并上传.因为要兼容安卓4.4以下版本的手机,所 ...

随机推荐

  1. apache ab

    ab -p postfile.json -T 'application/json' -n 100 -c 10 -v 2 http://192.168.1.103:3002/checkStashSlot ...

  2. postgresql-shared_buffers调整

    shared_buffers大小调整: http://www.rummandba.com/2011/02/sizing-sharedbuffer-of-postgresql.html SELECT  ...

  3. Ubuntu系统的安装(虚拟机) 并配置C/C++编译器

    一.系统的初始化配置 1.配置静态IP和DNS 配置静态IP 1.sudo vim /etc/network/interfaces,修改文件内容如下: auto eth0  #表示让网卡开机自动挂载e ...

  4. POJ 2575

    #include<iostream> #include<set> #include<stdio.h> using namespace std; int my_abs ...

  5. GCC C语言 DLL范例,含源码

    作者:小白救星 编译:gcc -c -DBUILDING_HZ_DLL1 hzdll1.c           gcc -shared -o hzdll1.dll hzdll1.o -Wl,--kil ...

  6. 以太坊ERC20代币合约案例

    一.ERC20代币合约与web3调用 ERC20代币合约在小白看来觉得很高大上,但其实就是一个代币的定义标准,方便其他dapp统一调用各种代币的方法.如图: 二.ERC20合约标准 [官方链接] co ...

  7. CentOS安装与配置Powerline插件

    Powerline powerline 可用于美化终端和vim编辑器的插件,它是Python开发的,为多个应用(bash,zsh,tmux等)提供statusline. 下面我们在CentOS上为vi ...

  8. logstash使用template提前设置好maping同步mysql数据到Elasticsearch5.5.2

    上篇blog说到采用logstash-input-jdbc将mysql数据同步到ES(http://www.cnblogs.com/jstarseven/p/7704893.html),但是这里有一个 ...

  9. 自然语言处理--jieba和gensim的分词功能

    一.jieba分词功能 1.主要模式 支持三种分词模式: 精确模式,试图将句子最精确地切开,适合文本分析: 全模式,把句子中所有的可以成词的词语都扫描出来, 速度非常快,但是不能解决歧义: 搜索引擎模 ...

  10. Linux-(ping,traceroute,ss)

    ping命令 1.命令格式: ping [参数] [主机名或IP地址] 2.命令功能: ping命令用于:确定网络和各外部主机的状态:跟踪和隔离硬件和软件问题:测试.评估和管理网络.如果主机正在运行并 ...