我正在为我的游戏管创建问答系统。当我试图将图像上传到qstimages(图像上传路径文件夹名称)时,在提交页面的问题上有些奇怪。看看我拍的照片。
this is image 1 - this is the normal condition before upload image
这是我用于图片上传功能的编码
questionstart.php(html代码)
<div class="container" id="contain">
<div class="row">
<div class="col-md-8 content">
<div class="form-group">
<form action="func/function.php" method="POST" enctype="multipart/form-data">
<br />
<label for="usr"> Ask anything about Games and Movies , Tv shows here : <span> Dont have an account , Post as a Guest </span> <?php //echo $pleasefill;?> </label>
<input type="text" name="qst" autocomplete="off" class="form-control" id="usr" placeholder="Enter your question here " required/>
<textarea class="form-control textarea" name="qsttextarea" rows="5" id="usr_dis" placeholder="Describe what is wrong ....." onKeyDown="textCounter(this.form.qsttextarea,this.form.countDisplay);" onKeyUp="textCounter(this.form.qsttextarea,this.form.countDisplay);" required></textarea>
<hr>
<div class="container" style="width: 100%;
margin-left: -15px;
border: 1px solid #e1e1e100;
margin: 2px;
background: #f1f1f1;
padding-bottom: 10px;">
<br />
<input type="file" name="file" id="file" />
<br />
<span id="uploaded_image"></span>
</div>
<hr>
<div class="col-md-12" id="div_tablesplitter">
<div class="col-md-6">
<input type="text" name="moreabout" class="form-control" id="more" placeholder="Give a short text about the questions" required/>
</div>
<div class="col-md-6 pull-right">
<select>
<option value="volvo">Action </option>
<option value="saab"> Act-adven </option>
<option value="saab"> Adventure </option>
<option value="saab"> Role play </option>
<option value="saab"> Simulation </option>
<option value="saab"> Strategy </option>
<option value="saab"> Sports </option>
<option value="saab"> Other </option>
</select>
</select>
<input readonly type="text" class="countDisplay" name="countDisplay" size="3" maxlength="3" value="3000">
</div>
</div>
<input type="submit" name="submit" value="Submit question" id="submit_button">
<input type="reset" value="Reset" onclick="clearText()" class="reset" />
</form>
</div>
js文件链接到questionstart.php
$(document).ready(function(){
$(document).on('change', '#file', function(){
var name = document.getElementById("file").files[0].name;
var form_data = new FormData();
var ext = name.split('.').pop().toLowerCase();
if(jQuery.inArray(ext, ['gif','png','jpg','jpeg']) == -1)
{
alert("Invalid Image File");
}
var oFReader = new FileReader();
oFReader.readAsDataURL(document.getElementById("file").files[0]);
var f = document.getElementById("file").files[0];
var fsize = f.size||f.fileSize;
if(fsize > 5000000)
{
alert("Image File Size is very big");
}
else
{
form_data.append("file", document.getElementById('file').files[0]);
$.ajax({
url:"questionstart.php",
method:"POST",
data: form_data,
contentType: false,
cache: false,
processData: false,
beforeSend:function(){
$('#uploaded_image').html("<label class='text-success'>Image Uploading...</label>");
},
success:function(data)
{
$('#uploaded_image').html(data);
}
});
}
});
});
包含在questionstart.php中的php代码
<?php
//upload.php
error_reporting(0);
if($_FILES["file"]["name"] != '')
{
$test = explode('.', $_FILES["file"]["name"]);
$ext = end($test);
$name = rand(100, 999) . '.' . $ext;
$location = './qstimages/' . $name;
move_uploaded_file($_FILES["file"]["tmp_name"], $location);
echo '<img src="'.$location.'" height="100" width="100" class="img-thumbnail" />';
}
?>
更新(新错误)
when i refresh the page an error message shown , please check the image
答案 0 :(得分:0)
在我看来服务器正在使用表单的html而不是预期的图像html响应你的ajax调用。看着你的ajax电话,我想我明白为什么。网址不应该是'upload.php'而不是'questionstart.php'吗?
$.ajax({
url:"upload.php",
method:"POST",
data: form_data,
contentType: false,
cache: false,
processData: false,
beforeSend:function(){
$('#uploaded_image').html("<label class='text-success'>Image Uploading...</label>");
},
success:function(data)
{
$('#uploaded_image').html(data);
}
});
答案 1 :(得分:0)
我认为问题在于您是(通过ajax)发布到同一页面,但是您没有将处理上传的代码部分与页面加载时运行的其余php脚本隔离,因此实际上ajax查询正在加载整个页面并将所有生成的HTML内容作为响应发送,而您只想返回特定于图像上载的数据。
对于你来说,下面代码的重要部分应该是顶部,首先是一个逻辑测试来检查请求是POST(也可以为其他POST字段添加更严格的逻辑测试) - 但是然后刷新任何以前生成的内容的缓冲区(如果在此之前有任何内容) - 处理上传,然后EXIT
/ DIE
停止加载剩余的HTML内容。
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['submit'] ) ){
/*
ensure that the response sent back to the ajax callback is
ONLY the data you want to send and not the entire page!
*/
# Flush the output buffers
ob_clean();
/*
This is where you would process the image upload
*/
/* Whatever data you need to send back to the ajax callback */
echo $response;
#EXIT to ensure response data is only generated here
exit();
}
?>
<!doctype html>
<html>
<head>
<title> - question start - </title>
</head>
<body>
<!-- html content -->
<!--
The html content includes the form and all
other page elements which are being repeated
after the ajax request has been sent.
-->
</body>
</html>