I am using Vue and element UI to achieve the upload file function, also use pdfvuer node module. In this case, the files will upload to Amazon S3 finally.
I want to preview the file before the user click confirms button. ref picture: upload
Currently, I used the blob and createObjectUrl method to realize preview PNG and JPG type files. but not work with PDF type ref picture: upload and preview png
Here is my code with upload of dialog:
HTML :
<span class="pt-0">
<p class="text-center mt-0">Please selecct file which you want 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 just upload PDF/PNG/JPG file only
</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(){ retrun ... }
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;
},
}
Can someone help me to resolve this issue? Either offer me another way to re-write code.
If it needs more detailed information on my code, please let me know.
Updated. I finally found out the solution to my question.
I just use the <embed> tag to reach the preview function.
Here is my code :
<input @change="handleChange" />
// preview area
<embed
v-if="embedSrc"
type="video/webm"
:src="embedSrc"
width="100%"
style="max-height: 50rem; min-height: 20rem"
/>
...
data:{
return {
embedSrc: ''
}
}
...
methods:{
handleChange(){
this.embedSrc = URL.createObjectURL(event.target.files[0]);
}
}
Please note that add : before src, either it can't get dynamic src correctly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With