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="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="./upload.js"></script>
    <style>
        #drop img{ 100px;height: 100px;margin: 10px;}
    </style>
</head>
<body onload="test()">
    <div id="drop" style=" 500px;height: 400px;background: #eee;margin: 0 auto;">
        <div style="clear: both;"></div>
    </div>
    <p style="text-align: center"><button onclick="up()">提交</button></p>
 
</body>
</html>

  

3.引入js

js地址 :http://files.cnblogs.com/files/jiebba/upload.js

4.引用插件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
var formData = new FormData(),list={}
     function test() {
         var d = new DragUpLoads()
         d.getDragImage({id:'drop',dropCallback:function (data) {
             if(list[data.name]) return;
             list[data.name] = true
             formData.append("files", data.blob);
             formData.append("asdfas"'asdfasdf');
             document.getElementById('drop').appendChild(data.img)
             /*
             * 返回img对象,url ,blob对象
             * */
         }})
     }
     function up() {
         console.log(formData)
        /*
        * formData  这个对象即我们要传的值
        * 通过 异步post/get 给后台即可
        * */
     }

  

代码仅供参考,具体功能可以自己扩展。

http://www.cnblogs.com/jiebba    我的博客,来看吧!

原文地址:https://www.cnblogs.com/libin-1/p/6533791.html