6

I am creating a web app in which I want my user to download the file which they are selected in input type="file"

here is my html

<input type='file' id='fieldID' onchange="return ValidateFileUpload('fieldID')"/>

now my JS

function ValidateFileUpload(ID) {

    var fuData = $('#' + ID);
    var FileUploadPath = fuData[0].value;

    //To check if user upload any file
    if (FileUploadPath == '') {

    } else {
        var Extension = FileUploadPath.substring(
            FileUploadPath.lastIndexOf('.') + 1).toLowerCase();

        //The file uploaded is an image

        if (Extension == "gif" || Extension == "png" || Extension == "bmp"
            || Extension == "jpeg" || Extension == "jpg" || Extension == "pdf" || Extension == "ppt" || Extension == "pptx" || Extension == "doc" ||Extension == "docx"
            || Extension == "xls" || Extension == "xlsx") {

            var file = $('#' + ID)[0].files[0];
            var filename = $('#' + ID)[0].files[0].name;
            var blob = new Blob([file]);
            var url = URL.createObjectURL(blob);

            $(this).attr({ 'download': FileUploadPath, 'href': url });
            filename = "";
        }

        //The file upload is NOT an image
        else {
            alert("Document is not the correct format: pdf,ppt,pptx,doc,docx,xls,xlsx and txt are the only document types allowed for upload. Please try again.");

        }
    }
}

but I am not able to download the selected file, can you please help me out to download the file selected in file upload

2
  • You need to upload it server before download Commented Oct 16, 2018 at 7:53
  • Are you trying to upload this file to your server? Your current terminology makes no sense as it implies you want the user to download a file they already have. Commented Oct 16, 2018 at 7:56

1 Answer 1

4

This should do the trick.

<script>
    function DownloadFile() {
        file = input.files[0];
        fr = new FileReader();
        fr.readAsDataURL(file);

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

        var objectURL = window.URL.createObjectURL(blob);
        console.log(objectURL);

        if (navigator.appVersion.toString().indexOf('.NET') > 0) {
            window.navigator.msSaveOrOpenBlob(blob, 'myFileName.pdf');
        } else {
            var link = document.createElement('a');
            link.href = objectURL;
            link.download = "myFileName.pdf";
            document.body.appendChild(link);
            link.click();
            link.remove();
        }
    }
</script>

<input type="file" id="input" />
<input type='button' value='Download' onclick='DownloadFile();'>

Check: https://utilitiesforprogrammers.blogspot.com/2019/01/download-file-from-input-type-file.html

Sign up to request clarification or add additional context in comments.

4 Comments

hi @Nuno i tries this example. and i read the code theoretically it should work but it is not working for me do you have any idea why it is not work out. also i tried the href link in the url it is not working
The input id was wrong (my bad). I also changed the script and added code to work in IE.
i am using firefox 67.0.4 (64-bit) it is not working and in IE , i tried to upload pdf and try to open it . it is not opening it says some error in the file
The problem is the click in firefox in JS ... do this: ... link.download = "myFileName.pdf"; document.body.appendChild(link); link.click(); link.remove(); ... i alter the code also :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.