简体   繁体   English

原型js:ajax提交中缺少输入类型文件

[英]Prototype js : input type file is missing on ajax submit

I have create a varien custom form and want to submit the form using prototype ajax.This form contain four fields 我已经创建了一个varien自定义表单,并希望使用原型ajax提交表单。此表单包含四个字段

two text field and 
two file field.

But when i submit the data using ajax of prototype js the form did not passed two field and on enctype="multipart/form-data" but does not works. 但是当我使用原型j的ajax提交数据时,表单没有通过两个字段并且在enctype =“multipart / form-data”上但是不起作用。 Code: 码:

<form action="bt" method="post" enctype="multipart/form-data" name="new-art-upload" id="new-art-upload">
<input type="text" name="fname" value=""   class="input-text required-entry"/>
<input type="text" name="fname" value="" class="input-text required-entry"  />
<input type="file" name="fileone"  class="required-entry"   />
<input type="file" name="filetwo" class="required-entry"   />
<button type="submit" title="<?php echo $this->__('Save The Art') ?>"  class="button newAdd_Sub" onclick="newartUpload.submit(this)"><span><span><?php echo $this->__('Save Art') ?></button>
</form>

Script: 脚本:

<script>
    var newartUpload=new VarienForm('new-art-upload');
    newartUpload.submit=function(button,url){
    if(this.validator.validate) {
        var form=this.form;
        var oldUrl = form.action;
        if (url) {
           form.action = url;
        }
        var e=null;
        try{
          // this.form.submit();
           new Ajax.Request(this.form.action,{
            method:this.form.method,
            parameters:this.form.serialize(),
                    contentType: 'multipart/form-data',

            onSuccess:function(transport){
                var response=transport.responseText.evalJSON(true);

            }.bind(this)
            });


        }catch(e){
        }
        if(e){
            throw e;
        }
    }
    }.bind(newartUpload)
</script>

I guess that it may content type and mostly content type in form/ 我猜它可能是内容类型,主要是内容类型的形式/

Main issue is that files input fields are not sended to ajax request 主要问题是文件输入字段未被发送到ajax请求

You can't submit a file using Prototype through Ajax, because XMLHttpRequest (the foundation of Ajax) does not work with a multipart form. 您无法通过Ajax使用Prototype提交文件,因为XMLHttpRequest(Ajax的基础)不适用于多部分表单。 There are work-arounds, such as using a keyhole iframe to send a normal form request, and using a callback through the iframe to redirect or react the outer page to show that the form completed. 有一些解决方法,例如使用keyhole iframe发送普通表单请求,以及通过iframe使用回调来重定向或响应外部页面以显示表单已完成。 The new File interface in modern JS (standardized years after the Prototype Ajax interface was written) now makes it possible to send file data through a JS submission, but this was actively discouraged (and only possible at all in a tiny handful of browsers, not too long ago) so Prototype just drops file inputs from the list of form elements it will serialize. 现代JS中的新File接口(在Prototype Ajax接口编写完成后的标准化年份)现在可以通过JS提交发送文件数据,但这是积极的劝阻(并且只能在极少数浏览器中使用,而不是很久以前)所以Prototype只是从它将序列化的表单元素列表中删除文件输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM