最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - File encryption using CryptoJS - Client side - Stack Overflow

matteradmin5PV0评论

I am trying to encrypt all files within a folder located on client side. I have the below code, but getting an error. I am not entirely sure about the error.

ERROR: Uncaught TypeError: Cannot read property 'length' of undefined(…) at line 16 in html.

index.html

<!DOCTYPE html>
<html>
<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>Get Directory</title>
    <script src=".10.2/jquery.min.js"></script>
    <script src="assets/js/aes.js"></script>
    <script type="text/javascript"> 
    $(document).ready(function(){
        $("#file-input").on("change", function(e){
            var thefiles = e.target.files;
            var reader = new FileReader();
            $.each(thefiles, function(i, item){
                var thefile = item;
                reader.onload = function(){
                    var encrypted = CryptoJS.AES.encrypt(thefile, '12334');
                };
                reader.readAsDataURL(thefile);
                $("#thelist").append("FILES: " + thefile.name + "<br />");;
            });
        });
    });
    </script>
</head>
<body>
    <input type="file" id="file-input" webkitdirectory="" directory="">
    <div id="thelist"></div>
</body>
</html>

I am trying to encrypt all files within a folder located on client side. I have the below code, but getting an error. I am not entirely sure about the error.

ERROR: Uncaught TypeError: Cannot read property 'length' of undefined(…) at line 16 in html.

index.html

<!DOCTYPE html>
<html>
<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>Get Directory</title>
    <script src="http://cdnjs.cloudflare./ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="assets/js/aes.js"></script>
    <script type="text/javascript"> 
    $(document).ready(function(){
        $("#file-input").on("change", function(e){
            var thefiles = e.target.files;
            var reader = new FileReader();
            $.each(thefiles, function(i, item){
                var thefile = item;
                reader.onload = function(){
                    var encrypted = CryptoJS.AES.encrypt(thefile, '12334');
                };
                reader.readAsDataURL(thefile);
                $("#thelist").append("FILES: " + thefile.name + "<br />");;
            });
        });
    });
    </script>
</head>
<body>
    <input type="file" id="file-input" webkitdirectory="" directory="">
    <div id="thelist"></div>
</body>
</html>
Share Improve this question edited Nov 7, 2016 at 21:45 Endless 38.2k13 gold badges116 silver badges137 bronze badges asked Nov 7, 2016 at 20:38 NoobNoob 671 gold badge5 silver badges14 bronze badges 2
  • Think of what you're doing in the $.each – Adam Wolski Commented Nov 7, 2016 at 20:46
  • Looping through the files in selected folder. – Noob Commented Nov 7, 2016 at 20:58
Add a ment  | 

1 Answer 1

Reset to default 2

Read all ments

<!DOCTYPE html>
<html>
<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>Get Directory</title>
    <!-- Update your jQuery version??? -->
    <script src="http://cdnjs.cloudflare./ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="assets/js/aes.js"></script>
    <script> // type="text/javascript" is unnecessary in html5

    // Short version of doing `$(document).ready(function(){`
    // and safer naming conflicts with $
    jQuery(function($) { 

        $('#file-input').on('change', function() {

            // You can't use the same reader for all the files
            // var reader = new FileReader

            $.each(this.files, function(i, file) {

                // Uses different reader for all files
                var reader = new FileReader

                reader.onload = function() {
                    // reader.result refer to dataUrl
                    // theFile is the blob... CryptoJS wants a string...
                    var encrypted = CryptoJS.AES.encrypt(reader.result, '12334')
                }

                reader.readAsDataURL(file)
                $('#thelist').append('FILES: ' + file.name + '<br>')
            })
        })
    })
    </script>
</head>
<body>
    <input type="file" id="file-input" webkitdirectory="" directory="">
    <div id="thelist"></div>
</body>
</html>

btw, browsers has a modern standard Crypto lib built in... Maybe try using that instead? and if necessary use a polyfill?

Post a comment

comment list (0)

  1. No comments so far