我正在尝试使用javascript代码在parse.com上进行多次上传。 我已经设法将一个图像上传到一个类中,但现在我希望将多个图像上传到不同的行中。
我试过这个
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="http://www.parsecdn.com/js/parse-latest.js"></script>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<script type="text/javascript">
$(document).ready(function() {
Parse.initialize("APPID", "JSKEY");
function saveJobApp(objParseFile) {
var jobApplication = new Parse.Object("imagemagazia");
jobApplication.set("imagename", objParseFile);
jobApplication.save(null, {
success: function(gameScore) {
alert("succesfull save image");
},
error: function(gameScore, error) {
alert('Failed to create new object, with error code: ' + error.description);
}
});
}
$('#submitId').on("click", function(e) {
PhotoUpload();
});
function PhotoUpload() {
var test = [];
console.log("edw");
var fileUploadControl = $("#profilePhotoFileUpload")[0];
for (i=0; i<3; i++){
var file = fileUploadControl.files[i];
//test.push(file);
var name = file.name; //This does *NOT* need to be a unique name
console.log(name +" : "+file);
//}
console.log(test);
//var t = [];
//for(j=0; j<test.length; j++){
var parseFile = new Parse.File(name, file);
//t.push(parseFile);
//var nw = t[i];
parseFile.save().then(
function() {
saveJobApp(parseFile);
},
function(error) {
alert("error");
}
);
}
}
});
</script>
</head>
<body>
<form id="business_form" method="post">
<table>
<tr>
<td>Image</td>
<td><input type="file" id="profilePhotoFileUpload" multiple></td>
</tr>
<tr>
<td><input type="button" id="submitId" value="submit"></td>
</tr>
</table>
</form>
</body>
</html>
更新。使用上面的代码我试图上传3张图片。 “1.jpg”“2.jpg”“3.jpg”
但它只上传3.jpg,因为它是最后一个。 有什么想法吗?
答案 0 :(得分:2)
我注意到最好在外部循环函数本身,而不是在函数中创建循环。 如果你尝试这样做:
$('#submitId').on("click", function(e) {
var fileUploadControl = $("#profilePhotoFileUpload")[0];
console.log(fileUploadControl.length);
for (i=0; i<3; i++){
var file = fileUploadControl.files[i];
var name = file.name;
PhotoUpload(name, file);
}
});
然后:
function PhotoUpload(objname, objfile) {
console.log(objname);
console.log(objfile);
var parseFile = new Parse.File(objname, objfile);
parseFile.save().then(
function() {
saveJobApp(parseFile);
},
function(error) {
alert("error");
}
);
}
另外,如果想要添加多个树图像,只需添加:
for (i=0; i<fileUploadControl.files.length; i++)