通过ajax上传文件的结果是php://input
---解决 通过替换通过ajax上传文件的结果是php://input,php,jquery,ajax,apache,Php,Jquery,Ajax,Apache,---解决 通过替换 $.ajax({ // create an AJAX call... type: $(this).attr('method'), // GET or POST url: $(this).attr('action'), // the file to call processData: false, cache: false, contentType: false, data: formData, // get the form
$.ajax({ // create an AJAX call...
type: $(this).attr('method'), // GET or POST
url: $(this).attr('action'), // the file to call
processData: false,
cache: false,
contentType: false,
data: formData, // get the form data
beforeSend: function() {
$(".dynamicContentWindow").load("loading.php"); // hides the window during load
},
success: function(data, status) { // on success..
$(".dynamicContentWindow").html(data); // update the DIV
contentLoaded(); //recall the catchers for the new page
},
});
与:
解决了这个问题。然而,我不明白为什么,我认为在另一篇文章中也没有解释。如果有人能指出这一点,那将是一种极大的解脱
大家好,, 我对使用jQuery非常陌生,目前正在尝试使用ajax建立一个异步文件上传。然而,我的请求总是以失败告终php://input 并且不会传递给$\u POST或$\u文件。昨天我在谷歌上搜索了整整一天,并尝试了几种方法,但我无法找出问题所在。使用的服务器是Apache2.2.9(是的,它实际上已经为博物馆做好了准备,但这不是我的选择) 在本例中,html代码为:
<form action="ua_import_files.php" method="POST" enctype="multipart/form-data" class="file">
<input type="hidden" name="tab_select" value="0" />
<input type="hidden" name="MAX_FILE_SIZE" value="9999000000" />
<input type="hidden" name="file_type" value="planview" />
<input name="uploadedfile" type="file" size="40" style="background:#D0D0D0; font-size:10px; margin-top:7px;" /></p>
<br/>
<br/>
<input type="submit" value="Upload File" style="font-size:12px; width: 100px; margin-top:7px; float:left;display:block;" />
</form>
因此,console.log()也会生成一个输出。
但是,如果我在服务器上转储$\u文件或$\u POST,这两种情况下都会得到空数组。但是,当转储文件\u获取\u内容时(“php://input)我明白了:
string(2941129) "-----------------------------29431251527956 Content-Disposition: form-data; name="tab_select" 0 -----------------------------29431251527956 Content-Disposition: form-data; name="MAX_FILE_SIZE" 9999000000 -----------------------------29431251527956 Content-Disposition: form-data; name="file_type" exp -----------------------------29431251527956 Content-Disposition: form-data; name="uploadedfile"; filename="file.exp" Content-Type: application/octet-stream EXP op 2015-08-13T00:00:00.000Z 2016-01-01T00:00:00.000Z XXX XXX ..."
(我只知道这里的一些字符串)
我已经检查了配置文件,它应该为服务器提供足够的资源来处理请求:
memory_limit = 128M;
max_execution_time = 3600;
max_input_time = 600;
upload_max_filesize = 30M
post_max_size = 100M
我假设,我的头有问题,导致服务器无法解析数据,或者配置中有另一个错误,我还没有发现。有人有主意吗 试试这段代码。给你的id表单id var formData=new formData($('#yourformid')[0]) $.ajax({
调用phpinfo();并查看upload_max_filesize override or not。您的代码有一些问题:
if($(this).attr('class') == "file") { // <---what $(this) is here
$(this).submit(function(e){ // what $(this) in here? is it one of many forms?
您无法在控制台中看到所有内容,但最好检查浏览器的“网络”选项卡,您可以在标题中看到有关已发送表单数据的所有信息。可能重复的
$(此)。提交(函数(e){
这里的$(此)
是什么?这表示表单
?您有多少表单?我添加了$(“form”).each(function(){});在解释$(this)语句的代码中。我尝试过这样做,但在尝试调用form.append('file',form.files[0]时);我得到错误:TypeError:formData.files未定义。我还认为,通过将此文件传递给构造函数,该文件已添加到formData中,还是我错了?您是否尝试过不附加它?我猜您是对的,因为表单位于formData中,因此不需要附加。是的,我尝试过不附加它,但仍然是相同的错误.他们到了,但只在晚上php://input.Oky,我认为它默认会这样做,谢谢,然后我会这样做:)
memory_limit = 128M;
max_execution_time = 3600;
max_input_time = 600;
upload_max_filesize = 30M
post_max_size = 100M
type: $(this).attr('method'),
url: $(this).attr('action'),
data: formData,
async: false,
cache: false,
processData: false,
contentType: false,
beforeSend: function() {
$(".dynamicContentWindow").load("loading.php"); // hides the window during load
},
success: function(data, status) { // on success..
your code
}
});
if($(this).attr('class') == "file") { // <---what $(this) is here
$(this).submit(function(e){ // what $(this) in here? is it one of many forms?
var form = new FormData(this); // <---what this belogs here.
$('.file').submit(function(e) { // submit the actual form
e.preventDefault();
var form = new FormData(this); // pass this as a reference to the form
form.append('file', form.files[0]); //<----append the file here
$.ajax({
type: $(this).attr('method'), // <----form method as type in ajax
url: $(this).attr('action'), // <-----form action as a url in ajax
beforeSend: function() {
$(".dynamicContentWindow").load("loading.php");
},
success: function(data, status) {
$(".dynamicContentWindow").html(data);
contentLoaded();
},
data: form, //<------------sending form which have `file` as a key to the file name
processData: false,
contentType: false,
cache: false
});
});
console.log(form);