2

I'm trying to make a multiple upload in parse.com with javascript code. I have managed to upload one image into a class but now i want multiple images to be uploaded in different rows.

I have tried this

<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>

UPDATE. with the above code i am trying to upload 3 images. "1.jpg" "2.jpg" "3.jpg"

but it uploads only 3.jpg because its the last one. Any idea?

0

1 Answer 1

2

I noticed it's better to make a loop the function itself externally and not making a loop in the function. if you try making it like this :

$('#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);
     }
    });

And then :

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

Also if want to add more than tree images, you just add this :

for (i=0; i<fileUploadControl.files.length; i++)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.