1

Quite simply im trying to pass a file from javascript to PHP using javascript, Ive tried the following but its not picking up the file that ive inputted.

My Html

    <form v-on:submit="submitForm($event)">
        <input type="file" id="image" v-el="fileInput">
        <button class="btn">
            Submit
        </button>
    </form>

My JavaScript

methods: {
        submitForm: function(e) {
                e.preventDefault();

                var formData = new FormData();
                formData.append('image', $('#image').files[0]);

                console.log(formData);

                this.$http.post('getFiles', formData, function (data, status, request) {
                    console.log('success');
                }).error(function (data, status, request) {
                    console.log('error');
                });
            }
        }

And lastly The Error

TypeError- Cannot read property '0' of undefined

Im not 100% sure whats the problem here but it doesnt seem to be registering when i upload a file, any help would be appreciated

1 Answer 1

1

$('#image') is going to return an array.

So, to get it to work you can:

$('#image')[0].files[0]

Hope this helps!

Sign up to request clarification or add additional context in comments.

4 Comments

This fixes the error! but now how come no data is being passed still when i upload a .PNG file no data is being returned when i console.log it?
@KenziieeFlavius I'm glad I could help! If this has fixed the error please would you mark the answer as accepted. As for your no data is being returned question, there are too many possible answers and it goes outside of the scope of this question, however, I'd be happy to help if you post a new question.
When i get a bit more time ill put it forwards as a new question then link you to it if thats alright
@KenziieeFlavius Absolutely fine.

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.