案例:ajax利用FormData制作QQ相冊


<!DOCTYPE?html>

<html?lang="en">

<head>

????<meta?charset="UTF-8">

????<meta?name="viewport"?content="width=device-width,?initial-scale=1.0">

????<meta?http-equiv="X-UA-Compatible"?content="ie=edge">

????<title>Document</title>

????<link?rel="stylesheet"?href="css/photo.css">

</head>

<body>

????<!--?展示相關(guān)?-->

????<div?class="container">

????????<div?class="photoHeader">

????????????<div?class="imgContainer">

????????????????<img?class="photoName"?src="img/1.jpg"?/>

????????????</div>

????????????<div?class="btnContainer">

????????????????<span?class="photoTitle">相冊名稱</span>

????????????????<button?class="mybtn">上傳照片</button>

????????????</div>

????????</div>

????????<div?class="photoContainer">

????????????<div?class="photoItem">

????????????????<img?src="img/Home.png"?/>

????????????????<span>

????????????????????home

????????????????</span>

????????????</div>

????????</div>

????</div>

????<!--?上傳相關(guān)?-->

????<div?class="masking">

????????<div?class="addPhotoContainer"></div>

????????<div?class="addController">

????????????<h3?class="addTitle">上傳照片-普通上傳(H5)<span?class="close">╳</span></h3>

????????????<div?class="photoTitles">

????????????????<span?class="uploadTo">上傳到</span>

????????????????<div?class="photoSelect">

????????????????????<img?class="showPhoto"?src="img/1.jpg"?/>

????????????????????相冊名稱

????????????????</div>

????????????</div>

????????????<!--?上傳按鈕?-->

????????????<div?class="showContainer">

????????????????<div?class="uploadContainer">

????????????????????<span?class="fileinput-button">

????????????????????????<span>上傳圖片</span>

????????????????????????<input?class="imgFile"?type="file"?name=""?multiple="multiple"?/>

????????????????????</span>

????????????????????<span?class="hint">

????????????????????????按住Ctrl可多選

????????????????????</span>

????????????????</div>

????????????</div>

????????????<!--?顯示待上傳圖片??-->

????????????<div?class="loadContainer">

????????????????<div?class="wantUpload">

????????????????????<!--?<div?class="uploadPhotoItem">

????????????????????????<span?class="myProgress">

????????????????????????????<span?class="plan"></span>

????????????????????????????30%

????????????????????????</span>

????????????????????????<img?src="img/1.jpg"?/>

????????????????????????<span?class="pictureName">

????????????????????????????home

????????????????????????</span>

????????????????????</div>?-->

????????????????</div>

????????????????<div?class="addStyle">

????????????????????<span?class="fileinput-add">

????????????????????????<span></span>

????????????????????????<input?class="imgFile-add"?type="file"?multiple="multiple"?/>

????????????????????</span>

????????????????</div>

????????????????<!--?開始上傳按鈕?-->

????????????????<div?class="bottomStyle">

????????????????????<span?class="uploadBtn">開始上傳</span>

????????????????</div>

????????????</div>

????????</div>

????</div>

</body>

<script>

????//?function?createEle(item){

????//?????let?fileReader?=?new?FileReader();

????//?????????????fileReader.readAsDataURL(item);

????//?????????????fileReader.onload?=?function(){

????//?????????????????let?div?=?document.createElement("div");

????//?????????????????div.classList.add("uploadPhotoItem");

????//?????????????????div.innerHTML?=?`<span?class="myProgress">

????//?????????????????????????<span?class="plan"></span>

????//?????????????????????????30%

????//?????????????????????</span>

????//?????????????????????<img?src="${fileReader.result}"?/>

????//?????????????????????<span?class="pictureName">

????//?????????????????????????${item.name}

????//?????????????????????</span>`;

????//??????????????????document.querySelector(".wantUpload").appendChild(div);??

????//?????????????}

????//?}

????let?uploadArr?=?[];

????document.querySelector(".close").onclick?=?function?()?{

????????document.querySelector(".masking").style.display?=?"none";

????}

????document.querySelector(".mybtn").onclick?=?function?()?{

????????document.querySelector(".masking").style.display?=?"block";

????}

????document.querySelector(".imgFile").onchange?=?function?()?{

????????//?console.log(this.files);

????????document.querySelector(".showContainer").style.display?=?"none";

????????document.querySelector(".loadContainer").style.display?=?"block";

????????[...this.files].forEach(item?=>?{

????????????//?圖片轉(zhuǎn)換成base64格式的圖片姐浮;

????????????let?WillUploadObj?=?new?WillUploadImg(item);

????????????WillUploadObj.createEle();

????????????uploadArr.push(WillUploadObj);

????????????//?createEle(item);

????????})

????}

????document.querySelector(".imgFile-add").onchange?=?function?()?{

????????[...this.files].forEach(item?=>?{

????????????//?圖片轉(zhuǎn)換成base64格式的圖片业岁;

????????????//?createEle(item);

????????????let?WillUploadObj?=?new?WillUploadImg(item);

????????????WillUploadObj.createEle();

????????????uploadArr.push(WillUploadObj);

????????})

????}

????document.querySelector(".uploadBtn").onclick?=?function?()?{

????????console.log(uploadArr);

????????queue();

????????async?function?queue()?{

????????????for?(let?i?=?0;?i?<?uploadArr.length;?i++)?{

????????????????await?uploadArr[i].uploadImg();

????????????}

????????}

????????//??function?queue()?{

????????//?????uploadArr.forEach(async?function(){

????????//?????????await?val.uploadImg();

????????//?????})

????????//?}

????}

????class?WillUploadImg?{

????????constructor(file)?{

????????????this.file?=?file;

????????????this.div?=?"";

????????}

????????createEle()?{

????????????//?創(chuàng)建節(jié)點順序霞怀;?

????????????let?fileReader?=?new?FileReader();

????????????fileReader.readAsDataURL(this.file);

????????????let?_this?=?this;

????????????fileReader.onload?=?function?()?{

????????????????let?div?=?document.createElement("div");

????????????????div.classList.add("uploadPhotoItem");

????????????????div.innerHTML?=?`<span?class="myProgress">

????????????????????????????<span?class="plan"></span>

????????????????????????????<span?class="exchange">30%?</span>

????????????????????????</span>

????????????????????????<img?src="${fileReader.result}"?/>

????????????????????????<span?class="pictureName">

????????????????????????????${_this.file.name}

????????????????????????</span>`;

????????????????document.querySelector(".wantUpload").appendChild(div);

????????????????_this.div?=?div;

????????????}

????????}

????????uploadImg()?{

????????????return?new?Promise((resolve,?reject)?=>?{

????????????????let?form?=?new?FormData();

????????????????form.append("img",?this.file);

????????????????let?xhr?=?new?XMLHttpRequest();

????????????????xhr.open("post",?"/upload",?true);

????????????????xhr.onload?=?function?()?{

????????????????????console.log(xhr.responseText);

????????????????}

????????????????xhr.upload.onprogress?=?(evt)?=>?{

????????????????????console.log(evt.total,?evt.loaded);

????????????????????//?上傳的進(jìn)度邮利;

????????????????????let?percent?=?Math.round(evt.loaded?/?evt.total?*?100)?+?"%";

????????????????????//?console.log(percent);

????????????????????this.div.querySelector(".myProgress").style.display?=?"block";

????????????????????this.div.querySelector(".plan").style.width?=?percent;

????????????????????this.div.querySelector(".exchange").innerHTML?=?percent;

????????????????????if?(evt.loaded?===?evt.total)

????????????????????????resolve();

????????????????}

????????????????xhr.send(form);

????????????})

????????}

????}

????//?作業(yè):1.完成上傳(普通上傳)2.繼續(xù)添加圖片烦磁;

????//?思考:1.面向?qū)ο蟪橄裆蟼鲗ο螅?.?依次上傳圖片;

????//?抽象上傳的對象;---》屬性:file;方法:1.創(chuàng)建節(jié)點歧蒋;2.上傳;

????//?1.登錄(區(qū)分用戶:登錄用戶只能看到自己上傳圖片cookie);2.完善圖片上傳谜洽;

</script>

</html>



const?Koa?=?require("koa");

const?fs?=?require("fs");

const?static?=?require("koa-static");

const?Router?=?require("koa-router");

//?接收post參數(shù)及上傳的文件萝映;

const?koaBody?=?require("koa-body");

let?app?=?new?Koa();

let?router?=?new?Router();

app.use(static(__dirname+"/static"));

app.use(koaBody({

????multipart:true

}))

router.get("/",ctx=>{

????ctx.body?=?"some?value";

})

router.post("/upload",ctx=>{

????let?filepath?=?ctx.request.files.img.path;

????if(!fs.existsSync("static/uploads/")){

????????fs.mkdirSync("static/uploads/");

????}

????//?流方式轉(zhuǎn)存?;?文件路徑阐虚;

????let?rs?=??fs.createReadStream(filepath);

????rs.pipe(fs.createWriteStream("static/uploads/"+ctx.request.files.img.name));

????ctx.body?={

????????info:"轉(zhuǎn)存成功",

????????status:1

????}

})

app.use(router.routes());

app.listen(3000);

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末锌俱,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子敌呈,更是在濱河造成了極大的恐慌,老刑警劉巖造寝,帶你破解...
    沈念sama閱讀 211,042評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件磕洪,死亡現(xiàn)場離奇詭異,居然都是意外死亡诫龙,警方通過查閱死者的電腦和手機(jī)析显,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,996評論 2 384
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來签赃,“玉大人谷异,你說我怎么就攤上這事〗趿模” “怎么了歹嘹?”我有些...
    開封第一講書人閱讀 156,674評論 0 345
  • 文/不壞的土叔 我叫張陵,是天一觀的道長孔庭。 經(jīng)常有香客問我尺上,道長,這世上最難降的妖魔是什么圆到? 我笑而不...
    開封第一講書人閱讀 56,340評論 1 283
  • 正文 為了忘掉前任怎抛,我火速辦了婚禮,結(jié)果婚禮上芽淡,老公的妹妹穿的比我還像新娘马绝。我一直安慰自己,他們只是感情好挣菲,可當(dāng)我...
    茶點故事閱讀 65,404評論 5 384
  • 文/花漫 我一把揭開白布富稻。 她就那樣靜靜地躺著,像睡著了一般己单。 火紅的嫁衣襯著肌膚如雪唉窃。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,749評論 1 289
  • 那天纹笼,我揣著相機(jī)與錄音纹份,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛蔓涧,可吹牛的內(nèi)容都是我干的件已。 我是一名探鬼主播,決...
    沈念sama閱讀 38,902評論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼元暴,長吁一口氣:“原來是場噩夢啊……” “哼篷扩!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起茉盏,我...
    開封第一講書人閱讀 37,662評論 0 266
  • 序言:老撾萬榮一對情侶失蹤鉴未,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后鸠姨,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體铜秆,經(jīng)...
    沈念sama閱讀 44,110評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,451評論 2 325
  • 正文 我和宋清朗相戀三年讶迁,在試婚紗的時候發(fā)現(xiàn)自己被綠了连茧。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,577評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡巍糯,死狀恐怖啸驯,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情祟峦,我是刑警寧澤罚斗,帶...
    沈念sama閱讀 34,258評論 4 328
  • 正文 年R本政府宣布,位于F島的核電站搀愧,受9級特大地震影響惰聂,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜咱筛,卻給世界環(huán)境...
    茶點故事閱讀 39,848評論 3 312
  • 文/蒙蒙 一搓幌、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧迅箩,春花似錦溉愁、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,726評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至奕塑,卻和暖如春堂污,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背龄砰。 一陣腳步聲響...
    開封第一講書人閱讀 31,952評論 1 264
  • 我被黑心中介騙來泰國打工盟猖, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留讨衣,地道東北人。 一個月前我還...
    沈念sama閱讀 46,271評論 2 360
  • 正文 我出身青樓式镐,卻偏偏與公主長得像反镇,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子娘汞,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,452評論 2 348

推薦閱讀更多精彩內(nèi)容