gpt4 book ai didi

php - 带有进度条错误的 HTML5 AJAX 文件 uploader php

转载 作者:行者123 更新时间:2023-11-28 02:06:04 25 4
gpt4 key购买 nike

我从这里制作了一个脚本,但现在我无法获取 $_POST['file']

她是编剧

索引.php

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="jquery.min.js" type="text/javascript"></script>
<script src="ajax.js" type="text/javascript"></script>
<style>
progress
{
background-color:#FF0000;
width:250px;
min-height:2px;
height:4px;
border:none;
}
progress::-webkit-progress-bar-value, progress::-webkit-progress-value, progress::-moz-progress-bar, progress::progress-bar
{
background-color:#0F0;
}
</style>
</head>

<body>
<form enctype="multipart/form-data" method="post">
<input name="file" type="file" id="file" />
<input type="button" value="Upload" />
</form>
<div id="prc"></div>%<br>
<progress value="0" min="0" max="100"></progress>
<div id="det"></div>
</body>
</html>

ajax.js

// JavaScript Document
$(function(){
$(':file').change(function(){
var file = this.files[0];
name = file.name;
size = file.size;
type = file.type;
$("#det").html("NAME: "+name+"<br/>SIZE: "+size+"<br/>TYPE: "+type);
//your validation
});

$(':button').click(function(){
var formData = new FormData($('form')[0]);
$("#data").html(formData);
$.ajax({
url: 'upload.php', //server script to process data
type: 'POST',
xhr: function() { // custom xhr
myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){ // check if upload property exists
myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // for handling the progress of the upload
}
return myXhr;
},
//Ajax events
//beforeSend: beforeSendHandler,
success: function(html) {
$("#mess").html(html);
},
error:function(html) {
$("#mess").html(html);
},
enctype: 'multipart/form-data',
// Form data
data: formData,
//Options to tell JQuery not to process data or worry about content-type
cache: false,
contentType: false,
processData: false
});
});
function progressHandlingFunction(e){
if(e.lengthComputable){
$('progress').attr({value:e.loaded,max:e.total});
$('#prc').html(e.loaded);
}
}
});

和“upload.php”脚本当我收到错误时

<?php
if(isset($_POST['file'])){
echo $_POST['file'];
}else{
echo 'file is not set';
}
?>

结果

NAME: ALISON LIMERICK - Where love lives.mp3
SIZE: 16584832
TYPE: audio/mp3

> file is not set

progresbarr 运行良好,但在 php 中出现问题

有人帮帮我吗??

最佳答案

在处理使用 Post 上传的文件时,您需要使用 $_FILES。您可以找到关于 $_FILES here 的信息.

有关处理文件上传的更多信息,请访问 link

关于php - 带有进度条错误的 HTML5 AJAX 文件 uploader php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11507437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com