html5中上传图片

时间:2023-04-11 23:01:56

从相册中选择图片上传

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" }
);
}

函数套函数,要分清楚当前这个函数到底有那些参数。拍照上传的方法如下:

//从摄像头中拍照
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("没有找到摄像头", function () { }, configManager.alerttip, configManager.alertCtip);
} }

注意这一句task.addFile("file://" + entry.fullPath, { key: "file" }); 前面要加上file://防止在ios下找不到图片路径。