【发布时间】:2011-04-19 05:13:53
【问题描述】:
我只需要通过<input type="file"> 标签上传图片文件。
现在,它接受所有文件类型。但是,我想将其限制为仅包含 .jpg、.gif 等的特定图像文件扩展名。
我怎样才能实现这个功能?
【问题讨论】:
-
服务器端使用什么技术?
-
标签: html
我只需要通过<input type="file"> 标签上传图片文件。
现在,它接受所有文件类型。但是,我想将其限制为仅包含 .jpg、.gif 等的特定图像文件扩展名。
我怎样才能实现这个功能?
【问题讨论】:
标签: html
使用输入标签的 accept 属性。要仅接受 PNG、JPEG 和 GIF,您可以使用以下代码:
<input type="file" name="myImage" accept="image/png, image/gif, image/jpeg" />
或者简单地说:
<input type="file" name="myImage" accept="image/*" />
请注意,这仅向浏览器提供关于向用户显示哪些文件类型的提示,但这很容易绕过,因此您也应该始终在服务器上验证上传的文件。
它应该可以在 IE 10+、Chrome、Firefox、Safari 6+、Opera 15+ 中运行,但在移动设备上的支持非常粗略(截至 2015 年),并且根据一些报告,这实际上可能会阻止某些移动浏览器上传任何内容完全可以,所以一定要好好测试你的目标平台。
【讨论】:
accept="file/csv, file/xls" 有效吗??
image/x-png?根据这个列表,它只是image/png。 iana.org/assignments/media-types/media-types.xhtml
使用这个:
<input type="file" accept="image/*">
适用于 FF 和 Chrome。
【讨论】:
这样使用
<input type="file" accept=".png, .jpg, .jpeg" />
它对我有用
【讨论】:
您可以将accept 属性用于<input type="file"> 阅读此文档http://www.w3schools.com/tags/att_input_accept.asp
【讨论】:
这可以通过
<input type="file" accept="image/*" />
但这不是一个好方法。您必须在服务器端编码以检查文件是否为图像。
检查图像文件是真实图像还是假图像
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
}
else {
echo "File is not an image.";
$uploadOk = 0;
}
}
更多参考,请看这里
http://www.w3schools.com/tags/att_input_accept.asp
http://www.w3schools.com/php/php_file_upload.asp
【讨论】:
步骤:
1.输入标签添加accept属性
2. 使用 javascript 进行验证
3. 添加服务器端验证以验证内容是否真的是预期的文件类型
对于 HTML 和 javascript:
<html>
<body>
<input name="image" type="file" id="fileName" accept=".jpg,.jpeg,.png" onchange="validateFileType()"/>
<script type="text/javascript">
function validateFileType(){
var fileName = document.getElementById("fileName").value;
var idxDot = fileName.lastIndexOf(".") + 1;
var extFile = fileName.substr(idxDot, fileName.length).toLowerCase();
if (extFile=="jpg" || extFile=="jpeg" || extFile=="png"){
//TO DO
}else{
alert("Only jpg/jpeg and png files are allowed!");
}
}
</script>
</body>
</html>
说明:
【讨论】:
使用 type="file" 和 accept="image/*" (或您想要的格式),允许用户选择具有特定格式的文件。但是您必须在客户端重新检查它,因为用户可以选择其他类型的文件。 这对我有用。
<input #imageInput accept="image/*" (change)="processFile(imageInput)" name="upload-photo" type="file" id="upload-photo" />
然后,在您的 javascript 脚本中
processFile(imageInput) {
if (imageInput.files[0]) {
const file: File = imageInput.files[0];
var pattern = /image-*/;
if (!file.type.match(pattern)) {
alert('Invalid format');
return;
}
// here you can do whatever you want with your image. Now you are sure that it is an image
}
}
【讨论】:
const file: File = imageInput.files[0]; 吗?此外,将 id 分配给输入也不是以传统方式完成的。
如果您想一次上传多张图片,您可以在输入中添加multiple 属性。
upload multiple files: <input type="file" multiple accept='image/*'>
【讨论】:
简单而强大的方式(动态接受)
将格式放在数组中,例如“image/*”
var upload=document.getElementById("upload");
var array=["video/mp4","image/png"];
upload.accept=array;
upload.addEventListener("change",()=>{
console.log(upload.value)
})
<input type="file" id="upload" >
【讨论】:
您可以添加特定类型的图像或其他文件类型并在您的代码中进行验证:
<input style="margin-left: 10px; margin-top: 5px;" type="file" accept="image/x-png,image/jpeg,application/pdf"
(change)="handleFileInput($event,'creditRatingFile')" name="creditRatingFile" id="creditRatingFile">
handleFileInput(event) {
console.log(event);
const file = event.target.files[0];
if (file.size > 2097152) {
throw err;
} else if (
file.type !== "application/pdf" &&
file.type !== "application/wps-office.pdf" &&
file.type !== 'application/pdf' && file.type !== 'image/jpg' && file.type !== 'image/jpeg' && file.type !== "image/png"
) {
throw err;
} else {
console.log('file valid')
}
}
【讨论】:
在html中;
<input type="file" accept="image/*">
这将接受所有图像格式,但不接受 pdf 或视频等其他文件。
但是如果你使用的是django,在django forms.py中;
image_field = forms.ImageField(Here_are_the_parameters)
【讨论】:
其他人的答案为 ReactJS 重构(钩子)
import React from 'react';
const ImageUploader = () => {
const handleImageUpload = (e) => {
// If no file selected, return
if (e.target.files.length === 0) return false;
const file = e.target.files[0];
// If no image selected, return
if (!/^image\//.test(file.type)) {
alert(`File ${file.name} is not an image.`);
return false;
}
// ...
};
return (
<>
<input type='file' accept='image/*' onChange={(e) => handleImageUpload(e)} />
</>
);
};
export default ImageUploader;
【讨论】: