admin管理员组文章数量:1332339
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
1 Answer
Reset to default 4This 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
本文标签: jqueryDownload file from input type file javascriptStack Overflow
版权声明:本文标题:jquery - Download file from input type file javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742328886a2454281.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论