简体   繁体   English

在上传之前,根据文件的扩展名在预览中显示不同的图像

[英]Display different images in preview based on the extension of files before upload in php

I am creating a form which includes file uploading. 我正在创建一个包含文件上传的表单。 The user can upload image or other file like docx, txt, pdf etc. If a user browse and selects an image then it shows its thumbnail. 用户可以上传图像或其他文件,例如docx,txt,pdf等。如果用户浏览并选择了图像,则它会显示其缩略图。 The problem is that if a user selects a pdf or doc file to upload, I want to display a default image based on the extension of file. 问题是,如果用户选择要上传的pdf或doc文件,我想基于文件的扩展名显示默认图像。 For eg. 例如。 if its a pdf file then image1, for docx it should display image2. 如果是pdf文件,则为image1,对于docx,应显示image2。

I am using this code. 我正在使用此代码。

<img id="blah" alt="your image" width="80" height="80" />
<input id="file" type="file" name='file' onchange="document.getElementById('blah').src = window.URL.createObjectURL(this.files[0])" />

Here is the image of the file field and preview. 这是文件字段和预览的图像。 When image is selected 选择图像时

PS: It is working fine with images. PS:图像效果很好。 I want to know how to display preview for other extensions. 我想知道如何显示其他扩展的预览。 Hope I am making myself clear. 希望我能说清楚。

Maybe this code helps 也许这段代码有帮助

<input type="file" id="image" accept="image/*" onChange="validate(this.value)"/>

function validate(file) {
    var ext = file.split(".");
    ext = ext[ext.length-1].toLowerCase();      
    var arrayExtensions = ["jpg" , "jpeg", "png", "bmp", "gif"];

    if (arrayExtensions.lastIndexOf(ext) == -1) {
        //insert an image with the use of the "ext" variable
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM