最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Uploading file on change of file input - Stack Overflow

programmeradmin0浏览0评论

I am trying to upload a file on change of file input but I can't get it to work because I am not so good with javascript.

I did search on google and found this: How can I upload files asynchronously?

I used it and tried to change it to how I want to use it. But I am getting the error

`Uncaught TypeError: Illegal invocation`

This is my function:

function uploadFile(formData){
    $.ajax({
        url: 'inc/ajax/uploadFile.php',  //Server script to process data
        type: 'POST',
        data: {'formData':formData},
        //Ajax events
        success: function(html){
            alert(html);
        }
    });
}

I call the function like this:

$("input:file").change(function(){
    var file = this.files[0];
    uploadFile(file);
})

And uploadFile.php

<?php
 $formData = $_GET['formData'];
 echo $formData;
?>

I am just testing and trying to return the file in php to see if I can get it to work. But I have no idea on how I have to call it in PHP or send it with AJAX. I know how to upload it with PHP once I can retrieve the $_FILES in PHP.

I am trying to upload a file on change of file input but I can't get it to work because I am not so good with javascript.

I did search on google and found this: How can I upload files asynchronously?

I used it and tried to change it to how I want to use it. But I am getting the error

`Uncaught TypeError: Illegal invocation`

This is my function:

function uploadFile(formData){
    $.ajax({
        url: 'inc/ajax/uploadFile.php',  //Server script to process data
        type: 'POST',
        data: {'formData':formData},
        //Ajax events
        success: function(html){
            alert(html);
        }
    });
}

I call the function like this:

$("input:file").change(function(){
    var file = this.files[0];
    uploadFile(file);
})

And uploadFile.php

<?php
 $formData = $_GET['formData'];
 echo $formData;
?>

I am just testing and trying to return the file in php to see if I can get it to work. But I have no idea on how I have to call it in PHP or send it with AJAX. I know how to upload it with PHP once I can retrieve the $_FILES in PHP.

Share Improve this question edited May 23, 2017 at 12:14 CommunityBot 11 silver badge asked Aug 22, 2013 at 16:19 user1646859user1646859 4
  • $_GET on the client & on the server $_POST? – cocco Commented Aug 22, 2013 at 16:24
  • $_GET['formData']; should be empty since you're sending a POST request... – Kevin B Commented Aug 22, 2013 at 16:24
  • Note, uploading files like this will NOT work in IE<9, or is it 10? – Kevin B Commented Aug 22, 2013 at 16:25
  • Sorry I changed it to POST instead. But am I supposed to call formData like I do? And thanks Kevin B I will keep that in mind. – user1646859 Commented Aug 22, 2013 at 16:30
Add a ment  | 

2 Answers 2

Reset to default 3

In addition to the other answer, add this to your ajax

contentType: false,
processData: false

like:

var formData = new FormData();
formData.append('formData', file);
$.ajax({
    url: 'inc/ajax/uploadFile.php',  //Server script to process data
    type: 'POST',
    data: formData,
    contentType: false,
    processData: false,
    //Ajax events
    success: function(html){
        alert(html);
    }
});

You should also think about people that may not have a newer browser, and just put a little error message, like:

if(window.FormData === undefined){
    alert('sorry buddy, your browser\'s too old!');
    return;
}

You need to sent the data as a FormData object

function uploadFile(file){
    var formData = new FormData();
    formData.append('formData', file);
    $.ajax({
        url: 'inc/ajax/uploadFile.php',  //Server script to process data
        type: 'POST',
        data: formData,
        //Ajax events
        success: function(html){
            alert(html);
        }
    });
}

http://blog.new-bamboo.co.uk/2012/01/10/ridiculously-simple-ajax-uploads-with-formdata

发布评论

评论列表(0)

  1. 暂无评论