Download file from input type file javascript


        <script>
            function downloadFile() {
                if (!window.File || !window.FileReader || !window.FileList || !window.Blob) {
                    alert('The File APIs are not fully supported in this browser.');
                    return;
                }

                input = document.getElementById('fileinput');
                if (!input) {
                    alert("Um, couldn't find the fileinput element.");
                }
                else if (!input.files) {
                    alert("This browser doesn't seem to support the `files` property of file inputs.");
                }
                else if (!input.files[0]) {
                    alert("Please select a file before clicking 'Load'");
                }
                else {
                    file = input.files[0];
                    fr = new FileReader();
                    fr.readAsDataURL(file);

                    var blob = new Blob([file], { type: "application/pdf" });// change resultByte to bytes

                    var link = document.createElement('a');
                    link.href = window.URL.createObjectURL(blob);
                    link.download = "myFileName.pdf";
                    link.click();
                }
            }
        </script>

        <input type="file" id="fileinput" />
        <input type='button' id='btnLoad' value='download' onclick='downloadFile();'>

Comments

Popular posts from this blog

POST an array of objects with $.ajax to C# WebMethod

Work with array of objects in browser local storage