How can i upload image with ajax in codeigniter?

前端 未结 4 878
隐瞒了意图╮
隐瞒了意图╮ 2020-12-16 06:46

I\'m trying to upload image with jQuery and ajax function, and how can I fetch all details of image file, like in php we use $_FILE()

Here is my code

4条回答
  •  一向
    一向 (楼主)
    2020-12-16 07:24

    You can to use FormData api in html5.

    Your form must be:

    Then jquery:

    function uploadImage() {
    
        if (typeof FormData !== 'undefined') {
    
            // send the formData
            var formData = new FormData( $("#formID")[0] );
    
            $.ajax({
                url : baseUrl + 'uploadImage',  // Controller URL
                type : 'POST',
                data : formData,
                async : false,
                cache : false,
                contentType : false,
                processData : false,
                success : function(data) {
                    successFunction(data);
                }
            });
    
        } else {
           message("Your Browser Don't support FormData API! Use IE 10 or Above!");
        }   
    }
    

    Then in the controller you will get the files in $_FILES array.

提交回复
热议问题