更多會員限定文章可以到patreon觀看
完整code可以到以下gist
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
img {
width: 150px;
}
</style>
</head>
<body>
<input type="file" id="file-uploader" accept="image/png" multiple="multiple"/>
<img id='preview'>
</body>
</html>
image/*
我們會用JSON來傳遞資料(也可以用form-data)
讀進來的每個檔案會是blob(Binary Large Object)
const fileUploader = document.querySelector('#file-uploader');
fileUploader.addEventListener('change', (e) => {
console.log(e.target.files); // get list of file objects
});
e.target.files
是fileList
function display_img(curFiles) {
const curFile = curFiles[0];
const reader = new FileReader();
// 這會在readAS後才執行
reader.onload = function (e) {
// console.log('file:', e.target.result); // base64
document.querySelector('#preview').src = e.target.result;
};
// to data url
reader.readAsDataURL(curFile);
}
img.src
能顯示url with base64
相較於buffer, blob保留了照片的資訊,像是檔名、MIME type等
function blob2buffer(blob) {
return new Promise((resolve, reject) => {
var arrayBuffer;
var fileReader = new FileReader();
fileReader.onload = function (event) {
arrayBuffer = event.target.result;
resolve(arrayBuffer);
};
fileReader.readAsArrayBuffer(blob);
return arrayBuffer;
});
}
先將blob轉成buffer,方便後續建成array
buffer不能直接操作,要先轉成指定type的array
arrayBuffer = await blob2buffer(pic);
fetch(url, {
method: 'POST',
// flask need to set header of json
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
item_id: 1,
format: 'png',
img: Array.from(new Uint8Array(arrayBuffer)),
}),
})
轉成array前要先將buffer轉成typed array
產生blob的url (不同於前面產base64的fileReader)
function display_arr_img(arr) {
document.querySelector('#preview').src = URL.createObjectURL(new Blob([new Uint8Array(arr)], {type: "image/png"}));
}
將array轉回typed array,再將其變回blob
這裡我們用python + flask作為例子
這裡我們用MySQL,然後照片的欄位type是blob,要注意blob有分可以存的size
TINYBLOB
: maximum length of 255 bytesBLOB
: maximum length of 65,535 bytes => 64kbMEDIUMBLOB
: maximum length of 16,777,215 bytesLONGBLOB
: maximum length of 4,294,967,295 bytesimport pymysql
connection = pymysql.connect(host='localhost', user='root', password='password', db='db01', charset='', cursorclass=pymysql.cursors.DictCursor)
注意pymysql不支援connection string
所需套件在requirements.txt內
存到blob的好處是不會讓照片被額外index (會導致檔案大小增幅30%)
@app.route('/upload/json/img_raw', methods=['POST'])
def raw_img():
json_data = flask.request.json
# save to db
with connection.cursor() as cursor:
sql = "UPDATE table1 SET `img_raw`=%s WHERE id=%s"
try:
# img現在是list, 可以用json string/ byterarray/ base64存進資料庫
cursor.execute(sql, (json.dumps(json_data['img']), json_data['item_id']))
connection.commit()
except Exception as err:
connection.rollback()
return flask.jsonify({'err': err})
# get a picture
sql = "SELECT * FROM table1"
cursor.execute(sql)
result = cursor.fetchone()
r = result[0]['img_raw']
r = json.loads(r.decode()) # utf-8, 也可用ascii
return flask.jsonify({'img': r})
存進mysql blob欄位後,會變成binary object
json.dumps(json_data['img']
json.loads(r.decode())