I am currently utilizing Vue along with the element UI to enable file upload functionality, and I am also incorporating the pdfvuer node module. The uploaded files are ultimately being sent to Amazon S3.
My goal is to allow users to preview the file before clicking the confirm button. For reference, please see the image: upload
Currently, I have managed to implement a preview feature for PNG and JPG file types using the blob and createObjectUrl method, but this method does not work for PDF files. For reference, please see the image: upload and preview png
Below is the code snippet that includes the upload dialog:
HTML :
<span class="pt-0">
<p class="text-center mt-0">Please select the file you wish to import.</p>
<el-upload
accept="image/png, image/jpeg, application/pdf"
class="avatar-uploader"
:show-file-list="true"
:before-upload="beforeAvatarUpload"
action=""
>
<i class="fas fa-cloud-upload-alt fa-2x my-8" v-if="objectURL == ''"></i>
<img :src="objectURL" width="100%" />
<pdf :src="imageUrl.webkitRelativePath" />
<div class="el-upload__text" v-if="objectURL == ''">Click here to execute</div>
<div class="el-upload__tip" slot="tip">
You can only upload PDF/PNG/JPG files.
</div>
</el-upload>
</span>
<span slot="footer" class="dialog-footer">
<el-button @click="innerDialogUploadFile = false">Cancel</el-button>
<el-button type="danger" @click="handleUploadFile">Confirm</el-button>
</span>
</el-dialog>
JS :
data(){ return ... }
methods:{
beforeAvatarUpload(file) {
this.objectURL = window.URL.createObjectURL(new Blob([file]));
this.imageUrl = window.URL.createObjectURL(new Blob([file]));
const isJPG = file.type === "image/jpeg";
const isPNG = file.type === "image/png";
const isPDF = file.type === "application/pdf";
const isLt2M = file.size / 1024 / 1024 < 20;
switch (file.type) {
case "image/png":
this.fileType = 1;
break;
case "image/jpeg":
this.fileType = 2;
break;
case "application/pdf":
this.fileType = 3;
break;
default:
break;
}
return isJPG && isPNG && isPDF && isLt2M;
},
}
I would greatly appreciate it if someone could assist me in resolving this issue or suggest an alternative approach to rewriting the code. If more detailed information about my code is required, please feel free to reach out.