<%@ Page Language="C#" AutoEventWireup="true" Title="上传照片" CodeBehind="Upload.aspx.cs" Inherits="Dotnet.Admin.Upload" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="../Javascript/jquery3.1.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
            <div style="margin:20px;">
       

                
        <input type="file" id="fileInput" name="fileInput" /> 
         
                

         <script>
             
             var reader = new FileReader();
             var img = new Image();
             var file = null;
             var _title = "";
             var canvas = document.createElement('canvas');
             var context = canvas.getContext('2d');
             var maxWidth = '1000';
             var maxHeight = '1500';



             fileInput.addEventListener('change', function (event) {
               
                 file = fileInput.files[0];
                 var name = file.name;
                 name = name.substring(0, name.lastIndexOf("."))
                 _title = name;
 
                 var filename = file.name;
                 var index = filename.lastIndexOf(".");
                 var suffix = filename.substr(index + 1);
                 suffix = suffix.toLowerCase();
                 if (suffix == "jpg" || suffix == "jpeg" || suffix == "png") {
                 }
                 else {
                     alert("图片格式不正确");
                     return;
                 }


                 if (file) {

                     reader.readAsDataURL(file);
                     reader.onload = function (e) {
                         var base64 = reader.result.split(',')[1];
                         var dataUrl = 'data:image/png;base64,' + base64;
                         img.src = dataUrl;
                     }


                     img.onload = function () {
                         var originWidth = this.width;
                         var originHeight = this.height;

                         var targetWidth = originWidth;
                         var targetHeight = originHeight;


                         if (originWidth > maxWidth || originHeight > maxHeight) {
                             if (originWidth / originHeight > maxWidth / maxHeight) {

                                 targetWidth = maxWidth;
                                 targetHeight = Math.round(maxWidth * (originHeight / originWidth));
                             } else {
                                 targetHeight = maxHeight;
                                 targetWidth = Math.round(maxHeight * (originWidth / originHeight));
                             }
                         }

                         // canvas对图片进行缩放
                         canvas.width = targetWidth;
                         canvas.height = targetHeight;
                         // 清除画布
                         context.clearRect(0, 0, targetWidth, targetHeight);
                         // 图片压缩
                         context.drawImage(img, 0, 0, targetWidth, targetHeight);


                         var base64 = canvas.toDataURL('image/jpeg', 1.0);


                         $.ajax({
                             url: "ajax/save_fm.aspx",
                             data: { base64: base64 },
                             type: "post",
                             cache: false,
                             async: false,
                             success: function (r) {
                                   
                                  window.location = "upload2.aspx?isself=<%=Request.QueryString["isself"]%>&orginFile=" + r; 

                             }
                         });



                     };

                 }

             });

         </script>











    </div>
    
    </div>
    </form>
</body>
</html>