Cakephp FormData Ajax 文件上传

问题描述 投票:0回答:1

我陷入了使用 FormData + Ajax + CakePHP 上传数据和文件的困境

这是我的表格:

        <form name="multiform" class="frmaddproduct" action="addproduct" method="POST" enctype="multipart/form-data">
        
        <div class="formrow">
            <div class="createproductlabel leftfloat">Name</div>
            <div class="formcontrol leftfloat"><input type="text" name="name"/><br/><span>Name?</span></div>
            <div class="clear"></div>
        </div>
        
        <div class="formrow">
            <div class="createproductlabel leftfloat">Cost</div>
            <div class="formcontrol leftfloat"><input type="text" name="cost"/></div>
            <div class="clear"></div>
        </div>
        
        <div class="formrow">
            <div class="createproductlabel leftfloat">Discount</div>
            <div class="formcontrol leftfloat"><input type="text" name="discount" class="discount" maxlength="5"/> %</div>
            <div class="clear"></div>
        </div>
        
        <div class="formrow">
            <div class="createproductlabel leftfloat">Description</div>
            <div class="formcontrol leftfloat"><textarea name="description" rows="5"></textarea></div>
            <div class="clear"></div>
        </div>
        
        <div class='formrow'>
            <div class='createproductlabel leftfloat'>Choose file</div>
            <div class='formcontrol leftfloat'><input type='file' name=''productpicture'/></div>
            <div class='closephotos leftfloat'><img src='../img/close.png' class='closeproductpicture'/></div>
            <div class='clear'></div>
        </div>
        
        <div class="formrow">
            <div class="createproductlabel leftfloat">&nbsp;</div>
            <div class="formcontrol leftfloat"><input type="button" name="btncreateproduct" value=" Create Product "/></div>
            <div class="clear"></div>
        </div>
        </form>

这是我提交此表单的代码:

$(".frmaddproduct").submit(function(e)
{
            var formObj = $(this);
            var formURL = formObj.attr("action");

        if(window.FormData !== undefined)  // for HTML5 browsers
            {
            
                var formData = new FormData(this);
                
                $.ajax({
                    url: formURL,
                    type: 'POST',
                    data:  formData,
                    mimeType:"multipart/form-data",
                    contentType: false,
                    cache: false,
                    processData:false,
                    success: function(data, textStatus, jqXHR)
                    {
                            $("#multi-msg").html('<pre><code>'+data+'</code></pre>');
                    },
                    error: function(jqXHR, textStatus, errorThrown) 
                    {
                        $("#multi-msg").html('<pre><code class="prettyprint">AJAX Request Failed<br/> textStatus='+textStatus+', errorThrown='+errorThrown+'</code></pre>');
                    }           
               });
                e.preventDefault();
                e.unbind();
           }
           else  // for older browsers
            {
                // generate a random id
                var  iframeId = 'unique' + (new Date().getTime());

                // create an empty iframe
                var iframe = $('<iframe src="javascript:false;" name="'+iframeId+'" />');

                // hide it
                iframe.hide();

                // set form target to iframe
                formObj.attr('target',iframeId);

                // Add iframe to body
                iframe.appendTo('body');
                iframe.load(function(e)
                {
                    var doc = getDoc(iframe[0]);
                    var docRoot = doc.body ? doc.body : doc.documentElement;
                    var data = docRoot.innerHTML;
                    $("#multi-msg").html('<pre><code>'+data+'</code></pre>');
                });
            
            }
});

在firebug中,我可以看到正在发送的数据:

-----------------------------2463414040929 Content-Disposition: form-data; name="categoryproduct" 1 -----------------------------2463414040929 Content-Disposition: form-data; 
name="companyproduct" 1 -----------------------------2463414040929 Content-Disposition: form-data; 
name="name" AAA -----------------------------2463414040929 Content-Disposition: form-data; name="cost" 34

但是当我尝试在控制器中读取或转储请求数据时,我得到:Array()

即没有数据正在发送。

debug($this->request->data)

另外,

$this->log($this->request->data["discount"])
给了我空白值

我哪里理解错了?

我没有使用 Cakephp 表单助手来创建表单。

ajax cakephp
1个回答
0
投票

使用常规的 $_FILES PHP 服务器变量

© www.soinside.com 2019 - 2024. All rights reserved.