PHP和Ajax文件上传 - 无法用$ _FILES的tmp_name的值文件上传、Ajax、PHP、_FILES

2023-09-10 19:57:07 作者:性感的猪蹄。

我想上传使用Ajax的文件,但我有麻烦处理文件... 出于测试目的,我已经建立一个简单的code,它是这样的:

I'm trying to upload a file using Ajax, but I'm having troubles handling the file... For test purposes I've build a simple code that looks like this:

记者:

xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST",document.getElementById('upload').action,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
var cmdStr="q="+str;
xmlhttp.send(cmdStr);

document.getElementById("ResponseDiv").innerHTML=xmlhttp.responseText;

PHP:

$q=$_POST["q"];
echo $q;

它工作正常, xmlhttp.responseText 打印 [目标文件]

我的问题,然而,就是我需要用 $的临时文件名_ FILES [Q] [tmp_name的值'] 。要做到这一点,我已经改变了code以下内容:

My problem, however, is that I need to get the temporary file name with $_FILES["q"]['tmp_name']. To do so I have changed the code to the following:

记者:

xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST",document.getElementById('upload').action,true);
xmlhttp.setRequestHeader("enctype","multipart/form-data");
var cmdStr="q="+str;
xmlhttp.send(cmdStr);

document.getElementById("ResponseDiv").innerHTML=xmlhttp.responseText;

PHP:

$q=$_FILES["q"]["tmp_name"];
echo $q;

问题是,现在 xmlhttp.responseText 我没有得到任何东西。 任何人都知道我在做什么错了?

Problem is that now with xmlhttp.responseText I don't get anything. Anyone knows what I'm doing wrong?

推荐答案

看看这个答案让文件上传使用AJAX。这是可能的,但在所有的浏览器不兼容。

Check out this answer for making file uploads with AJAX. It is possible, but not compatible in all browsers.

jQuery上传进度和AJAX文件上传

-

另外,如果你想在飞行上传,有一个很酷的图书馆,你可以得到所谓Uploadify。这是一个闪光/ jQuery的(或HTML5现在)钻井平台,允许你上传的飞行文件。在Flash版本,我最后一次使用它...您可以在回调函数添加到让它做你想要什么本质

Alternatively, if you want on the fly uploads, there is a cool library you can get called 'Uploadify'. It's a flash/jquery (or HTML5 now) rig that allows you to upload files on the fly. In the flash version, last time I used it... you can add in callback functions to make it do essentially anything you want.

一些聪明的JavaScript可以使这项工作给你。

Some clever javascript could make this work for you.

http://www.uploadify.com/

 
精彩推荐
图片推荐