Skip to content

Instantly share code, notes, and snippets.

@hrdyjan1
Last active October 18, 2023 05:45
Show Gist options
  • Select an option

  • Save hrdyjan1/d99f95fff2405c9d3e30b2d063ee2265 to your computer and use it in GitHub Desktop.

Select an option

Save hrdyjan1/d99f95fff2405c9d3e30b2d063ee2265 to your computer and use it in GitHub Desktop.
Download PDF
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>PDF upload/download</title>
</head>
<body>
<form>
<label for="pdf-upload">Choose a PDF file to upload:</label>
<input type="file" id="pdf-upload" name="pdf" accept=".pdf" />
<button type="button" onclick="uploadPdf()">Upload PDF</button>
</form>
<button type="button" onclick="downloadPdf()">Download PDF</button>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
let file;
function uploadPdf() {
const pdfInput = document.getElementById("pdf-upload");
file = pdfInput.files[0];
console.log(`Selected PDF file: ${file.name}`);
}
function downloadPdf() {
if (!file) {
console.error("Please select a PDF file first");
return;
}
const fileReader = new FileReader();
fileReader.onload = function () {
const pdfData = fileReader.result;
axios({
method: "post",
url: "/download-pdf",
data: pdfData,
responseType: "blob",
})
.then(function (response) {
const pdfUrl = window.URL.createObjectURL(
new Blob([response.data])
);
const a = document.createElement("a");
a.href = pdfUrl;
a.download = file.name;
a.click();
})
.catch(function (error) {
console.error(error);
});
};
fileReader.readAsArrayBuffer(file);
}
</script>
</body>
</html>
@adickdid69
Copy link

Giving me the following : VM982:1 Uncaught SyntaxError: Unexpected token '?'
While downloading https://drive.google.com/file/d/1KOBInuZNTzjuFdP_pd2XFcTpu4KD5Ngz/view?pli=1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment