怎么看与阿贾克斯的形式上传的文件? [英] How to see the Files uploaded in a form with ajax?

查看:120
本文介绍了怎么看与阿贾克斯的形式上传的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想上传文件发送的形式。我用PHP努力,但HTML和PHP之间我用JS和jQuery和Ajax(因为我不想让页面重新加载)。而且我有麻烦的$ _FILES。

I'm trying to upload a file send in a form. I'm trying it with php, but between html and php I use JS and Jquery and ajax (because I don't want the page to reload). And I'm having troubles with the $_FILES.

这是,我使用的一种形式(其中包含一个文件输入)与JavaScript的动作(ACTION =JavaScript的:发送presupMail();)

Here it is, I'm using a form (which contains a file input) with a javascript action (action="javascript: SendPresupMail();").

在那个JS功能我用一个小jQuery和AJAX,它里面有一个调用PHP函数。

In that JS function I use a little Jquery and ajax, inside it, there is a call to a php function.

问题是,在PHP函数中的$ _FILES是空的,我需要上传文件发送的形式。

The problem is that inside that php function the $_FILES is empty, and I need to upload the file send in the form.

下面是code:

HTML表单,打电话给JS:

HTML form, calling to JS:

<form action="javascript: sendPresupMail();" method="post" id="formId" enctype="multipart/form-data">
<input type="text" id="mail" name="mail" />
<input type="file" id="file_selected" name="file_selected" />
<input type="submit" value="Submit" />

JS函数,并调用到PHP使用AJAX和jQuery:

JS function, and call to PHP with AJAX and JQUERY:

function sendPresupMail() {
$.ajax({
    url: 'remote.php',
    type: 'post',
    data: {
    'w': 'sendPresupMail',
    'mail': document.getElementById('mail').value
    },
    success: function(data) {
        if(data != "ok" && data != ""){alert(data);}
        if(data == "ok"){alert("mail send.");}
    }
});

}

最后,PHP code:

Finally, the PHP code:

private function sendPresupMail(){
    $filename = ($_FILES['file_selected']['name']);
            ...
            ...
}

在code存在是无关的,问题是,$文件名没有收到任何东西,因为$ _FILES是空的(我检查了的var_dump,这是空的)。因此,我不能上传文件,我该怎么办?

The code there is irrelevant, the issue is that $filename is not receiving anything because $_FILES it's empty (I checked it with a var_dump, and it's empty). So I can not upload the file, what should I do?

解决

下面是解决方案: 其实这是一个比我想象simplier。首先,我创建一个iframe中,所以现在所有的表单,JavaScript中,阿贾克斯等的hapenning的iframe内。因此,它似乎是一个页面是不是令人耳目一新,因为IFRAME是做什么的。 感谢所有为你的答案呢!

Here is the solution: Actually it was a lot simplier than I thought. First, I create an iframe, so now all the form, javascript, ajax, etc. is hapenning inside the iframe. So it seems like the page is not refreshing, because the iframe is doing it. Thanks all for your answers anyway!

推荐答案

您不能单纯的Ajax / jQuery的做,但你可以用它支持的所有最新版本JavaScript的FORMDATA对象做组合主流浏览器。

You can't do it with pure Ajax/jQuery, but you can do it in combination with the JavaScript FormData object which is supported in all latest versions of the major browsers.

有一个非常简单的jQuery的例子可以在这里找到: https://开头coderwall。 COM / P / P-n7eq

A really simple jQuery example can be found here: https://coderwall.com/p/p-n7eq

有一个更详细的,但纯JavaScript,可以在这里找到:<一href="https://developer.mozilla.org/en-US/docs/Web/Guide/Using_FormData_Objects?redirectlocale=en-US&redirectslug=Web%2FAPI%2FFormData%2FUsing_FormData_Objects" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/Guide/Using_FormData_Objects?redirectlocale=en-US&redirectslug=Web%2FAPI%2FFormData%2FUsing_FormData_Objects

A more detailed, yet pure JavaScript, can be found here: https://developer.mozilla.org/en-US/docs/Web/Guide/Using_FormData_Objects?redirectlocale=en-US&redirectslug=Web%2FAPI%2FFormData%2FUsing_FormData_Objects

这篇关于怎么看与阿贾克斯的形式上传的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆