file上传图片html
1. html设置文件上传类型,如何设置在选择文件的时候只能选图片
可以设置一下html中的accept属性以实现上传文件类型的筛选,accept 属性只能与 <input type="file"> 配合使用。它规定能够通过文件上传进行提交的文件类型。
工具原料:编辑器、浏览器
1、设置一个文件上传选项,删选一下只能上传图片或者详细的限制只能上传图片的某些格式,代码如下:
<!DOCTYPEhtml>
<html>
<body>
<formaction="demo_form.asp">
<inputtype="file"name="pic"accept="image/*">
<inputtype="submit">
</form>
<p><strong>注释:</strong>InternetExplorer9以及更早的版本不支持input标签的accept属性。</p>
<p><strong>注释:</strong>鉴于安全考虑,该例不允许您上传文件。</p>
</body>
</html>
2、运行的结果是只能上传图片不能上传其他的文件,在弹出的上传选择对话框中也会值显示图片,如下图:
2. input file用了multiple属性 而且选择了多张图片 php该怎么上传
其实就那两个函数
is_uploaded_file()和move_uploaded_file()
是循环上传的。你打印下$_FILES这个超全局数组就明白了。下面我的测试代码上传图片的:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题</title>
</head>
<body>
<form action="test.php" method="post" enctype="multipart/form-data">
<input type="file" id="f" name="images[]" multiple="true" style="border:1px solid red" />
<input type="submit" value="提交" />
</form>
</body>
</html>
<?php
$path=$_SERVER['DOCUMENT_ROOT'].'test2/';
if(!empty($_FILES)){
echo "aaaa";
if(is_uploaded_file($_FILES['images']['tmp_name'][0])){
echo "1111";
exit;
}
foreach($_FILES['images']['tmp_name'] as $k=>$v){
if(is_uploaded_file($_FILES['images']['tmp_name'][$k])){
$save=$path.$_FILES['images']['name'][$k];
echo $save."<br>";
if(move_uploaded_file($_FILES['images']['tmp_name'][$k],$save)){
echo "上传成功!";
}
}
}
echo "<pre>";
print_r($_FILES);
echo "</pre>";
}
?>
3. html5拖拽图片上传,怎么获得图片原始尺寸
用后台语言去获取不就可以了。为什么一定要在前端来获取图片原始大小呢?即使你要在前端实用,比如PHP获取到了以后还是可以通过变量传递给前端的。至于PHP怎么获取图片原始大小这个网上很容易找到资料!
4. 拍照或从文件夹里上传图片(重复上传同一张图片失效的解决办法)
HTML:
<!--添加-->
<div class="dianji" @click="triggerUpload">添加图片</div>
<input type="file" id="upload" accept="image/*;capture=camera" value="" @change="upload">
JS:
triggerUpload() { //触发input的点击事件,用户选择图片进行上传
document.getElementById("upload").click();
},
//上传图片
upload(e) {
let self = this;
let file = e.target.files[0] || e.dataTransfer.files[0];
let Orientation;
//去获取拍照时的信息,解决拍出来的照片旋转问题
Exif.getData(file, function() {
Orientation = Exif.getTag(this, 'Orientation');
});
// 看支持不支持FileReader
if(!file || !window.FileReader) return;
if(/^image/.test(file.type)) {
// 创建一个reader
let reader = new FileReader();
// 将图片2将转成 base64 格式
reader.readAsDataURL(file);
// 读取成功后的回调
reader.onloadend = function() {
let result = this.result;
let img = new Image();
img.src = result;
e.target.value='';//input file 重复上传同一张图片失效的解决办法
let uploadImagesItem = {
msrc: this.result,
src: this.result,
Attachmentid: 0,
Newfilename: this.result
};
//现在是判断图片是否大于250k,是就直接上传,反之压缩图片
if(this.result.length <= (250 * 1024)) {
self.uploadImages.push(uploadImagesItem);
self.postImg(this.result); //提交图片到后台
} else {
img.onload = function() {
uploadImagesItem = {
msrc: self.compress(img, Orientation),
src: self.compress(img, Orientation),
Attachmentid: 0,
Newfilename: self.compress(img, Orientation)
};
self.uploadImages.push(uploadImagesItem);
let data = self.compress(img, Orientation);
self.postImg(data);
}
}
}
}
},
postImg() {
//这里写接口
},
//旋转照片, 利用exif.js解决ios手机上传竖拍照片旋转90度问题
rotateImg(img, direction, canvas) {
//最小与最大旋转方向,图片旋转4次后回到原方向
const min_step = 0;
const max_step = 3;
if(img == null) return;
//img的高度和宽度不能在img元素隐藏后获取,否则会出错
let height = img.height;
let width = img.width;
let step = 2;
if(step == null) {
step = min_step;
}
if(direction == 'right') {
step++;
//旋转到原位置,即超过最大值
step > max_step && (step = min_step);
} else {
step--;
step < min_step && (step = max_step);
}
//旋转角度以弧度值为参数
let degree = step * 90 * Math.PI / 180;
let ctx = canvas.getContext('2d');
switch(step) {
case 0:
canvas.width = width;
canvas.height = height;
ctx.drawImage(img, 0, 0);
break;
case 1:
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(img, 0, -height);
break;
case 2:
canvas.width = width;
canvas.height = height;
ctx.rotate(degree);
ctx.drawImage(img, -width, -height);
break;
case 3:
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(img, -width, 0);
break;
}
},
//压缩图片
compress(img, Orientation) {
let canvas = document.createElement("canvas");
let ctx = canvas.getContext('2d');
//瓦片canvas
let tCanvas = document.createElement("canvas");
let tctx = tCanvas.getContext("2d");
let initSize = img.src.length;
let width = img.width;
let height = img.height;
//如果图片大于四百万像素,计算压缩比并将大小压至400万以下
let ratio;
if((ratio = width * height / 4000000) > 1) {
console.log("大于400万像素")
ratio = Math.sqrt(ratio);
width /= ratio;
height /= ratio;
} else {
ratio = 1;
}
canvas.width = width;
canvas.height = height;
// 铺底色
ctx.fillStyle = "#fff";
ctx.fillRect(0, 0, canvas.width, canvas.height);
//如果图片像素大于100万则使用瓦片绘制
let count;
if((count = width * height / 1000000) > 1) {
console.log("超过100W像素");
count = ~~(Math.sqrt(count) + 1); //计算要分成多少块瓦片
// 计算每块瓦片的宽和高
let nw = ~~(width / count);
let nh = ~~(height / count);
tCanvas.width = nw;
tCanvas.height = nh;
for(let i = 0; i < count; i++) {
for(let j = 0; j < count; j++) {
tctx.drawImage(img, i * nw * ratio, j * nh * ratio, nw * ratio, nh * ratio, 0, 0, nw, nh);
ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh);
}
}
} else {
ctx.drawImage(img, 0, 0, width, height);
}
//修复ios上传图片的时候 被旋转的问题
if(Orientation != "" && Orientation != 1) {
switch(Orientation) {
case 6: //需要顺时针(向左)90度旋转
this.rotateImg(img, 'left', canvas);
break;
case 8: //需要逆时针(向右)90度旋转
this.rotateImg(img, 'right', canvas);
break;
case 3: //需要180度旋转
this.rotateImg(img, 'right', canvas); //转两次
this.rotateImg(img, 'right', canvas);
break;
}
}
//进行最小压缩
let ndata = canvas.toDataURL('image/jpeg', 0.1);
console.log('压缩前:' + initSize);
console.log('压缩后:' + ndata.length);
console.log('压缩率:' + ~~(100 * (initSize - ndata.length) / initSize) + "%");
tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0;
return ndata;
},
5. 用FileUpLoad控件上传图片后,如何将读取出的图片路径存储下来
参照以下代码,例子是直接将图片存入数据库,路径存储可以仿照
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList2.SelectedItem.Text.Trim() == "")
{
Label1.Text = "提示:请选择图片类型!!";
return;
}
if (FileUpload1.HasFile)
{
string fileContentType = FileUpload1.PostedFile.ContentType;
if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")
{
string name = FileUpload1.PostedFile.FileName; // 客户端文件路径
FileInfo file = new FileInfo(name);
string fileName = file.Name; // 文件名称
if (!File.Exists(webFilePath))
{
try
{
FileUpload1.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Provider=OraOLEDB.Oracle.1;Data Source=sdsfc;user id=shp;password=shp";
conn.Open();
string cmd = "update shp.CMCS_SFC_IMEIPRINT_FUNCTION set PICTURE_NAME=:PICTURENAME,PICTURE=:PICTURE where BARCODE_TYPE='" + DropDownList2.SelectedItem.Text.Trim() + "'";
OleDbCommand sql = new OleDbCommand(cmd, conn);
HttpPostedFile UpFile = FileUpload1.PostedFile;
int FileLength = UpFile.ContentLength;
Byte[] FileByteArray = new byte[FileLength]; //图象文件临时储存Byte数组
Stream StreamObject = UpFile.InputStream;//建立数据流对像
StreamObject.Read(FileByteArray, 0, FileLength);//读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
sql.Parameters.Add(":PICTURENAME", OleDbType.VarChar, 50).Value = fileName; //记录文件类型
sql.Parameters.Add(":PICTURE", OleDbType.LongVarBinary, FileLength).Value = FileByteArray;
sql.ExecuteNonQuery();
conn.Close();
Label1.Text = "提示:文件“" + fileName + "”成功上传";
}
catch (Exception ex)
{
Label1.Text = "提示:文件上传失败,失败原因:" + ex.Message;
}
}
else
{
Label1.Text = "提示:文件已经存在,请重命名后上传";
}
}
else
{
Label1.Text = "提示:文件类型不符";
}
}
}
6. html input标签 file类型,上传的具体是什么东西
没有什么限制,图片和文档都可以。
但如果你要限制文件类型也是可以的。
请参考这里:http://blog.csdn.net/wclxyn/article/details/7090575/