doudiejian5827 2015-06-23 09:36
浏览 44
已采纳

文件输入类型未包含在用于AJAX发送的jquery FormData中

I have a simple form that will be sent via AJAX to a php file for processing, this form includes a file upload, for some reason the file upload is not being included in the data to be sent.

My html form:

<form id="business-form">
                    <label for="business-name">Business Name:</label>
                    <input type="text" id="business-name" name="business-name">
                    <label for="business-location">Business Location:</label>
                    <input type="text" id="business-location" name="business-location">
                    <label for="business-description">Business Description:</label>
                    <input type="text" id="business-description" name="business-description">
                    <label for="fileToUpload">Business image:</label>
                    <input type="file" name="fileToUpload" id="fileToUpload">
                    <button id="create-business">Create New Business</button>
                </form>

My jquery:

function createNewBusiness(){
        var businessName = $('#business-name').val();
        var businessLocation = $('#business-location').val();
        var businessDescription = $('#business-description').val();
        var file_data = $('#fileToUpload').prop('files')[0];

        var sendAJAX = false;

        if (businessDescription.length > 1 && businessLocation.length > 1 && businessName.length > 1) sendAJAX = true;

        if (sendAJAX) {
            var formData = new FormData($("#business-form")[0]);
            var ft = $('#fileToUpload')[0].files[0];
            formData.append("fileToUpload", ft);
            console.log(ft);
            console.log(formData);
            $.ajax({
                url: '/app/business/save_created', // point to server-side PHP script
                dataType: 'text',  // what to expect back from the PHP script, if anything
                cache: false,
                contentType: false,
                processData: false,
                data: formData,
                type: 'post',
                success: function (php_script_response) {
                    alert(php_script_response); // display response from the PHP script, if any
                }
            });
        }else{
            alert("Please fill in all fields to create a new business")
        }
    }

The php script returns:

Array ( [business-name] => fefe [business-location] => fe [business-description] => fe )

When a file is selected, but if the file is left blank i get:

Array ( [business-name] => dede [business-location] => eafrea [business-description] => aefad [fileToUpload] => undefined )

the (cake)php that currently handles the ajax:

public function app_save_created(){
        $this->autoRender = false;
        $this->layout = 'ajax';

        print_r($this->request->data);
    }
  • 写回答

1条回答 默认 最新

  • dqudtskm49788 2015-06-23 10:15
    关注

    Your needs use enctype='multipart/form-data'.

    <form id="business-form" enctype='multipart/form-data'>
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题