最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Download file from input type file javascript - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question asked Oct 16, 2018 at 7:48 user10249871user10249871 2
  • You need to upload it server before download – Sumesh TG 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. – Rory McCrossan Commented Oct 16, 2018 at 7:56
Add a ment  | 

1 Answer 1

Reset to default 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./2019/01/download-file-from-input-type-file.html

发布评论

评论列表(0)

  1. 暂无评论