好久没写博客了,都感觉自己快堕落了!今天随性写一篇关于异步上传图片的程序及插件!

说是程序及插件,其实程序占大头,所谓的插件只是两个JS。分别为:jquery.html5upload.js 和 jquery.MultiFile.js 

下载地址为:https://files.cnblogs.com/files/chenwolong/upload.rar

其实也没什么好说的,直接上源代码吧!

HTML展示如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="LLYY.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="usercenter/js/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="usercenter/js/jquery-1.11.1.min.js"></script>
    <script src="usercenter/js/jquery.html5upload.js" type="text/javascript"></script>
    <script src="usercenter/js/jquery.MultiFile.js" type="text/javascript"></script>
    
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <input id="mainPicNum" name="mainPicNum" type="file"  class="" accept="gif|jpg|jpeg|png|bmp|pic" maxlength="1" onchange="change()"  />
        </div>
        <div id="result" style="padding-top:5px;">

        </div>
    </form>
</body>
</html>
<script type="text/javascript">
    $(function () {
        var result = document.getElementById("result");
        var input = document.getElementById("mainPicNum");

        if (typeof FileReader === 'undefined') {
            result.innerHTML = "抱歉,你的浏览器不支持 FileReader,请使用火狐浏览器,或其他兼容浏览器!";
            input.setAttribute('disabled', 'disabled');
        }

        $("#mainPicNum").MultiFile({
            afterFileSelect: function (element, value, master_element) {
                readFile.call(element);
            },
            afterFileRemove: function (element, value, master_element) {
                $('img').each(function () {
                    if ($(this).data('src') && (element.files[0].name == $(this).data('src'))) {
                        $(this).remove();
                    }
                });
            }
        });

        function readFile() {
            var file = this.files[0];
            if (!/image\/\w+/.test(file.type)) {
                alert("文件必须为图片!");
                return false;
            }
            var reader = new FileReader();
            reader.readAsDataURL(file);
            reader.onload = function (e) {
                result.innerHTML += '<img data-src="' + file.name + '" src="' + this.result + '" alt="" style=" height:100px; width:100px;"/>';
            }
        }
    });


    $('#mainPicNum').Html5Upload({
        url: 'UploadImage.ashx?action=action',
         perLoading: function (data, curindex) {
             // console.log(data)
             //$(".MultiFile-remove").css("display", "none");
             //$(".MultiFile-title").css("display", "none");
         },
         perLoaded: function (curindex, data) {
             //alert(data);
             //alert("上传头像成功");
             //var img = '<img src="/Images/foo.png" style="background-image: url(\'' + data + '\');" />';
             // $('#hiddenImg').val(data);
             //  $(".js_pic").html(img);
         }
    });

  
    </script>
View Code

相关文章:

  • 2021-08-01
  • 2022-12-23
  • 2022-12-23
  • 2019-10-19
  • 2022-12-23
  • 2022-01-04
  • 2021-08-01
  • 2021-09-09
猜你喜欢
  • 2022-02-07
  • 2022-12-23
  • 2021-08-03
  • 2021-10-28
  • 2022-01-05
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案