當前位置:首頁 » 文件管理 » file上傳圖片html

file上傳圖片html

發布時間: 2025-09-25 12:52:28

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/

熱點內容
哪個變數不編譯 發布:2025-09-25 15:44:58 瀏覽:107
酷我暢聽緩存位置 發布:2025-09-25 15:39:01 瀏覽:168
python後台開發 發布:2025-09-25 15:38:59 瀏覽:220
海康伺服器地址是多少啊 發布:2025-09-25 15:15:59 瀏覽:614
androidurl中文 發布:2025-09-25 15:02:09 瀏覽:532
iphone文件夾安裝目錄 發布:2025-09-25 14:54:26 瀏覽:466
無法清除dns緩存xp 發布:2025-09-25 14:53:12 瀏覽:8
少兒編程那個機構好 發布:2025-09-25 14:27:40 瀏覽:847
vcado資料庫使用 發布:2025-09-25 13:59:39 瀏覽:145
md解算布料需要什麼電腦配置 發布:2025-09-25 13:53:17 瀏覽:170