gpt4 book ai didi

JavaScript 错误 : "Uncaught TypeError: Cannot read property ' count' of undefined"in PHP file upload script

转载 作者:行者123 更新时间:2023-12-02 17:46:55 25 4
gpt4 key购买 nike

我的文件上传脚本(javascript 部分)中有一行给了我一个错误:

Uncaught TypeError: Cannot read property 'count' of undefined 

我的 javascript 中导致此问题的行是:

status.html(data.responseJSON.count + ' Files uploaded!').fadeIn();

这是我的完整 JavaScript 函数:

$(function() {
/* variables */
var status = $('.status');
var percent = $('.percent');
var bar = $('.bar');

/* submit form with ajax request using jQuery.form plugin */
$('form').ajaxForm({

/* set data type json */
dataType:'json',

/* reset before submitting */
beforeSend: function() {
status.fadeOut();
bar.width('0%');
percent.html('0%');
},

/* progress bar call back*/
uploadProgress: function(event, position, total, percentComplete) {
var pVel = percentComplete + '%';
bar.width(pVel);
percent.html(pVel);
},

/* complete call back */
complete: function(data) {
status.html(data.responseJSON.count + ' Files uploaded!').fadeIn();
}

});
});

该脚本使用 PHP 文件 uploader ,并使用 JSON 生成进度条。当我上传小文件时,该脚本可以工作,但当文件超过 10MB 时,无论 php.ini 设置如何(我在 php.ini 配置文件中将其设置为 500MB),它总是会给我一个错误。

使用 JavaScript 对文件大小是否有任何我不知道的限制?

编辑:根据@Rodrigo的要求,我有我的文件上传脚本以及下面的表单脚本,文件上传php代码位于最底部,并使用PHP_SELF和isset(submit)函数执行以减少数量使用的文件数量(所有内容均已记录):

<head>
<style type="text/css" media="screen">
div#banner_left {
position: absolute;
top: 0%;
left: 0%;
width: auto;
}
div#banner_right {
float: right;
width: auto;
}
</style>
<link rel="stylesheet" type="text/css" href="/css/structure.css">
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.form.min.js"></script>
<script type="text/javascript" src="js/uploader.js"></script>
</head>
<body>
<div id="banner_left">
<img src="js/logo_ryerson.gif"/>
</div>
<div class="status"></div>
<br><br><div class="img">
<img src="/images/logo.png" />
</div><center><br><br>
<form enctype="multipart/form-data" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="POST">
Choose Document Type:
<select name="document_type">
<option value="None">Pick Document Type</option>
<option value="Textbooks">Text Books</option>
<option value="Testbanks">Test Bank</option>
<option value="Notes & Assignments">Assignment/Notes</option>
</select><br>
Choose Course Code:
<select name="subject_list">
<option value="None">Pick Course</option>

<!-- FUNCTION TO SCAN DIRECTORY NAMES IN DROP DOWN LIST //-->

<?php
$dir = "./uploads";
$list = scandir($dir); /* This function sorts dirs */

$list = array_diff($list,array(".","..","index.php"));

foreach ($list as $file)
{
if (!is_dir($file)) echo "<option value='$file'>$file</option>\n";
}
?>
</select><br>
Choose file(s) to upload (Max 500MB): <input name="files[]" type="file" id="files" multiple="multiple" />
<input type="submit" name="submit" value="Upload" />
</form>
Upload Progress Bar<br>
<div class="progress">
<div class="bar"></div>
<div class="percent">0%</div>
</div>
<H2>All Courses</H2></center><br>

<!-- THE FOLLOWING CODE IS FOR LISTING DIRECTORIES INSIDE UPLOADS FOLDER //-->

<?php
$dir = "./uploads";
$list = scandir($dir); /* This function sorts dirs */
$list = array_diff($list,array(".","..","index.php"));

echo "<ol>";
foreach ($list as $file)
{
if (!is_dir($file)) echo "<li><a href='https://rye-high.ca/Rye High/$dir/$file'>$file</a></li>\n";
}
echo "</ol>";
?>
<!--FILE UPLOAD SCRIPT //-->

<?php
if(isset($_POST['submit']))
{
$subject_list = $_POST['subject_list'];
$document_type = $_POST['document_type'];
$dir = "/var/www/rye-high-website/Rye High/uploads/$subject_list/$document_type/";
$count = 0;

// Error if no course code is picked
if ($subject_list == "None") {
echo '<br><div class="alert">Please Pick A Course Code</div>';
}
// Error No Document Type is picked
if ($document_type == "None"){
echo '<br><div class="alert">Please Pick A Document Type</div>';
}

if ($_SERVER['REQUEST_METHOD'] == 'POST' and isset($_FILES['files']))
{
// loop all files
foreach ( $_FILES['files']['name'] as $i => $name )
{
// if file not uploaded then skip it
if ( !is_uploaded_file($_FILES['files']['tmp_name'][$i]) )
continue;

// now we can move uploaded files
if( move_uploaded_file($_FILES["files"]["tmp_name"][$i], $dir . $name) )
$count++;
}

echo json_encode(array('count' => $count));

}
}
?>

最佳答案

您的完整函数应该带有第二个字符串参数,以避免您尝试从未定义的属性中捕获属性。

你的第二个参数定义:

string categorizing the status of the request ("success", "notmodified", "error", "timeout", "abort", or "parsererror").

尝试这样的事情:

/* complete call back */
complete: function(data, status) {
if(status === "success")
status.html(data.responseJSON.count + ' Files uploaded!').fadeIn();
}

我认为您的问题来自 php,您应该向我们展示您的 php 代码或 php 问题。

关于JavaScript 错误 : "Uncaught TypeError: Cannot read property ' count' of undefined"in PHP file upload script,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21655643/

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