html5中上傳圖片
從相冊(cè)中選擇圖片上傳
function uploadFromAlbum(type) { var dirtype = ""; if ("pick_store_license" == type || "pick_id_pic" == type) { dirtype = "auth"; } if ("pick_store_pic" == type) { dirtype = "store"; } plus.gallery.pick( function (path) { //選擇成功 $("#heisebg").removeClass("heisebg").addClass("heisebghid"); $("#waitingupload").removeClass("heisebghid").addClass("heisebg"); var task = plus.uploader.createUpload(configManager.RequstUrl + "api/common/upload", { method: "POST", blocksize: 102400, priority: 100 }, function (upload, status) { // 上傳完成 if (status == 200) { var data = JSON.parse(upload.responseText); //顯示圖片
... ... $("#waitingupload").removeClass("heisebg").addClass("heisebghid"); } else { console.log("Upload failed: " + status); } } ); task.addFile( path, { key: "file" }); task.addData("dir", dirtype); task.start(); }, function (e) { console.log(e); }, { filter: "image" } ); }
函數(shù)套函數(shù),要分清楚當(dāng)前這個(gè)函數(shù)到底有那些參數(shù)。拍照上傳的方法如下:
//從攝像頭中拍照 function uploadFromCamera(type) { var dirtype = ""; if ("pick_store_license" == type || "pick_id_pic" == type) { dirtype = "auth"; } if ("pick_store_pic" == type) { dirtype = "store"; } var cmr = plus.camera.getCamera(1); if (null != cmr) { //拍照 cmr.captureImage(function (p) { plus.io.resolveLocalFileSystemURL( p, function (entry) { //拍照成功 $("#heisebg").removeClass("heisebg").addClass("heisebghid"); $("#waitingupload").removeClass("heisebghid").addClass("heisebg"); //上傳圖片 var task = plus.uploader.createUpload(configManager.RequstUrl + "/api/common/upload", { method: "POST", blocksize: 102400, priority: 100 }, function (upload, status) { // 上傳完成 if (status == 200) { var data = JSON.parse(upload.responseText); //顯示圖片
... ... console.log(upload.responseText); } else { console.log("Upload failed: " + status); } } ); task.addFile("file://" + entry.fullPath, { key: "file" }); task.addData("dir", dirtype); task.start(); }, function (e) { plus.ui.alert(e.message, function () { }, configManager.alerttip, configManager.alertCtip); } ); }, function (e) { }, { filename: "_doc/camera/" }); } else { plus.ui.alert("沒(méi)有找到攝像頭", function () { }, configManager.alerttip, configManager.alertCtip); } }
注意這一句task.addFile("file://" + entry.fullPath, { key: "file" }); 前面要加上file://防止在ios下找不到圖片路徑。
作者:Tyler Ning
出處:http://www.rzrgm.cn/tylerdonet/
本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁(yè)面明顯位置給出原文連接,如有問(wèn)題,請(qǐng)微信聯(lián)系冬天里的一把火
浙公網(wǎng)安備 33010602011771號(hào)