简体   繁体   English

通过Ajax将文件上传到php

[英]Uploading file to php through ajax

My form uses javascript alerts to communicate with the user as this is the preferred method in the company I work for (as opposed to constant redirects to and from the php handler file). 我的表单使用javascript警报与用户进行交流,因为这是我工作的公司中的首选方法(而不是与php处理程序文件进行不断重定向)。

Because of this, I pass all my form data through some simple validation in jquery and send it on to the php handler via ajax. 因此,我将所有表单数据通过jquery中的一些简单验证传递,并通过ajax发送到php处理程序。 Here's a basic look at how I'm doing it... 这是我的基本做法...

var first_name = $(sender + ' #first_name');
var email = $(sender + ' #email');
var tel = $(sender + ' #telephone');
var comments = $(sender + ' #comments');

$.ajax({
    type: 'POST',
    url: 'sendmail.php',
    data: { first_name: first_name.val(),
        email: email.val(),
        telephone: tel.val(),
        comments: comments.val(),
        success: function clearFields() {
            first_name.val('');
            email.val('');
            tel.val('');
            comments.val('');
            alert('Thank you. We will contact you as soon as possible.');
        }
    }
});

Having added an file input field to one such form as follows, I'm having trouble with the upload. 将文件输入字段添加到如下所示的一种表单中后,我在上传时遇到了麻烦。 While the email sends correctly, I don't think the ajax is sending any usable data for the upload on to the php file 虽然电子邮件正确发送,但我认为ajax不会发送任何可用数据以上传到php文件

<input type="file" name="upload" id="upload" />

<script>

var upload = $("#upload");
$.ajax({
    type: 'POST',
    url: 'sendmail.php',
    data: { first_name: first_name.val(),
        email: email.val(),
        telephone: tel.val(),
        upload: upload.val(),
        comments: comments.val(),
        success: function clearFields() {
            first_name.val('');
            email.val('');
            tel.val('');
            upload.val('');
            comments.val('');
            alert('Thank you. We will contact you as soon as possible.');
        }
    }
});
</script>

I've found a number of options for this, but all the ones I've looked at such as this seem "hackish" to me. 我发现了一些这个选项,但所有的人我看过像这样似乎“的hackish”给我。

Is there a simpler way to do this? 有没有更简单的方法可以做到这一点?

Ajax does not support file upload operation. Ajax不支持file上传操作。 But there are many plugins which make use of iframes to upload files asynchronously. 但是,有许多插件可以利用iframe异步上传文件。 You can read more about this technique here . 您可以在此处阅读有关此技术的更多信息。

Few jQuery plugins which supports form uploads are 很少有支持表单上传的jQuery插件是
1. jQuery form 1. jQuery表单
2. jQuery-File-Upload 2. jQuery文件上传

There are a lot of question answers regarding this in many Q&A sites, like this one . 在许多Q&A站点中,有很多与此相关的问题答案,例如这个

Another solution using html 5 is discussed here which uses FormData. 这里讨论使用html 5的另一个解决方案,该解决方案使用FormData。

You have to do this through an IFrame 您必须通过IFrame进行此操作

So you create a hidden iframe 因此,您创建了一个隐藏的iframe

<iframe id="upload_target" name="upload_target" style="display: none;" src="#"></iframe>
<!-- note the src="#" -->

Then you create a form with some button and all fields you wish to have 然后,创建一个带有一些按钮以及您希望拥有的所有字段的表单

<form action="path/to/uploadscript.php" method="POST" enctype="multipart/form-data" target="upload_target">
<!--target will tell the browser to run it in the iFrame with name="upload_target" -->

then in uploadscript.php you can use all form values as if they are regular $_POST values: 然后在uploadscript.php中,您可以使用所有表单值,就像它们是常规的$ _POST值一样:

<?php upload_file($_FILES['file'], $_POST['name'], $_POST['whatever']); ?>

This almost feels the same as using AJAX. 这几乎与使用AJAX一样。

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

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