简体   繁体   English

使用输入类型文件提交Ajax表单

[英]Ajax form submit with input type file

I am using ajax to submit a form that browses, uploads and then processes a file using PHP. 我正在使用ajax提交表单,该表单可以浏览,上传并使用PHP处理文件。 I hate the standard html input type="file" button (as most people do) so used a trick to click the standard "browse" button from another button (id="fileSelect") that I can eventually customize. 我讨厌标准的html input type =“ file”按钮(就像大多数人一样),所以使用了一个技巧来从另一个我可以自定义的按钮(id =“ fileSelect”)中单击标准的“​​浏览”按钮。 Works great in safari and others, but with IE the ajax code line that submits the form does not fire. 在Safari和其他浏览器中效果很好,但是使用IE提交表单的ajax代码行不会触发。 Other elements of the ajax code are executed (I get the alert("Ajax complete"); at the end), but the "$("#imageform").ajaxForm({target: '#preview'}).submit();" 执行ajax代码的其他元素(最后得到alert(“ Ajax complete”);),但是执行“ $(”#imageform“)。ajaxForm({target:'#preview'})。submit( );” line does not work in IE. 线在IE中不起作用。

Ajax code: Ajax代码:

$(document).ready(function() 
{       
    $('#xfile').live('change', function()   // when there is an even on the 'file' element (like file selected)
    { 
        $("#preview").html('');  // clears the preview area
        $("#preview").html('<img src="general_images/loading.gif" alt="Uploading...."/>');  // displays the loading
        $("#imageform").ajaxForm({target: '#preview'}).submit();
        alert ("Ajax complete");
    });
}); 

And HTML Code: 和HTML代码:

<div>
<button id="fileSelect" onclick="document.getElementById('xfile').click();">Insert Picture</button>
<form style="display: inline" id="imageform" name="imageform" action="upload_file_journal.php" method="post" target="_blank" enctype="multipart/form-data">
    <input style="visibility: hidden" type='file' name='xfile' id='xfile' /
</form>

This will not work in IE 9 and older(I think it works in IE 10,11). 这在IE 9及更高版本中不起作用(我认为在IE 10,11中有效)。

The only real option is to use absolute positions to place your upload control on top of your button, then set the opacity to 0.1, then they look like they are clicking a button, but are actually clicking the input, you have to super size the file input to ensure they always click the button part. 唯一的真实选择是使用绝对位置将上传控件放置在按钮上方,然后将不透明度设置为0.1,然后它们看起来就像在单击按钮,但实际上是在单击输入,因此必须超大尺寸文件输入,以确保他们始终单击按钮部分。

Even doing that is a hassle though. 即使这样做也很麻烦。 Hope this helps 希望这可以帮助

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

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