Javascript 通过ajax请求上传文件数据
我需要通过我的表单上传文件,但我不知道如何将文件上传合并到我的表单中以发布博客条目。 我不知道如何用JQuery执行ajax。 我不知道如何通过ajax请求发送图像文件/数据,如有任何帮助,将不胜感激 这就是我们的atm机: CP.PHPJavascript 通过ajax请求上传文件数据,javascript,ajax,jquery,file-upload,Javascript,Ajax,Jquery,File Upload,我需要通过我的表单上传文件,但我不知道如何将文件上传合并到我的表单中以发布博客条目。 我不知道如何用JQuery执行ajax。 我不知道如何通过ajax请求发送图像文件/数据,如有任何帮助,将不胜感激 这就是我们的atm机: CP.PHP <?php include("../scripts/database_connx.php"); include("../scripts/functions.php"); start_secure_session(); if
<?php
include("../scripts/database_connx.php");
include("../scripts/functions.php");
start_secure_session();
if(logged_in($sqli_con) === false) {
header("Location: ../index.php");
exit();
}
$cp = true;
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<title>TridantDesigns | Admin</title>
<link rel="stylesheet" type="text/css" href="../style/reset.css" />
<link rel="stylesheet" type="text/css" href="../style/main.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
setInterval(function() {
if(window.XMLHttpRequest) {
get_entries = new XMLHttpRequest();
} else {
get_entries = new ActiveXObject("Microsoft.XMLHTTP");
}
get_entries.onreadystatechange = function() {
if(get_entries.readyState == 4 && get_entries.status == 200) {
document.getElementById("r_e").innerHTML = get_entries.responseText;
}
}
get_entries.open("GET", "get_entries.php", true);
get_entries.send();
}, 50000);
});
</script>
</head>
<body>
<?php include("../scripts/includes/header.inc.php"); ?>
<div id="site_content">
<div id="new_entry">
<form>
<h1>Add A New Blog Entry</h1>
<input type="text" name="entry_title" maxlength="40" placeholder="Entry Title(40 Char)" /><br />
<textarea name="entry_contents" placeholder="Entry Content" /></textarea><br />
<label for="image">Choose Image File:</label>
<input type="file" name="image" /><br />
<input type="button" value="Post Entry" onclick="post_entry(this.form, this.form.entry_title, this.form.entry_contents, this.form.image);" />
</form>
<div id="post_error">
</div>
</div>
<div id="remove_entry">
<h1>Remove Entries(Click to remove)</h1>
<div id="r_e">
<?php
get_entries($sqli_con);
?>
</div>
</div>
</div>
<script>
function post_entry(form, title, contents, image) {
document.getElementById("post_error").style.display = "block";
if(title.value.length < 1) {
document.getElementById("post_error").innerHTML = "Please enter an entry title!";
return;
}
if(title.value.length > 40) {
document.getElementById("post_error").innerHTML = "Title can not be longer than 40 characters!";
return;
}
if(contents.value.length < 1) {
document.getElementById("post_error").innerHTML = "Please enter some content!";
return;
}
if(window.XMLHttpRequest) {
post_entry_ = new XMLHttpRequest();
} else {
post_entry_ = new ActiveXObject("Microsoft.XMLHTTP");
}
post_entry_.onreadystatechange = function() {
if(post_entry_.readyState == 4 && post_entry_.status == 200) {
document.getElementById("post_error").innerHTML = post_entry_.responseText;
if(post_entry_.responseText == "<response>Entry Added</response>") {
//get_entries();
}
}
}
post_entry_.open("POST", "add_entry.php", true);
post_entry_.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
post_entry_.send("title="+title.value+"&contents="+contents.value+"&image="+image.value);
}
function remove_entry() {
}
</script>
<?php include("../scripts/includes/footer.inc.php"); ?>
</body>
</html>
三叉戟设计|管理
$(文档).ready(函数(){
setInterval(函数(){
if(window.XMLHttpRequest){
get_entries=new XMLHttpRequest();
}否则{
get_entries=newActiveXObject(“Microsoft.XMLHTTP”);
}
get_entries.onreadystatechange=函数(){
if(get_entries.readyState==4&&get_entries.status==200){
document.getElementById(“r_e”).innerHTML=get_entries.responseText;
}
}
get_entries.open(“get”,“get_entries.php”,true);
获取_条目。发送();
}, 50000);
});
添加新的博客条目
选择图像文件:
删除条目(单击以删除)
功能post_条目(形式、标题、内容、图像){
document.getElementById(“post_错误”).style.display=“block”;
如果(title.value.length<1){
document.getElementById(“post_错误”).innerHTML=“请输入条目标题!”;
返回;
}
如果(title.value.length>40){
document.getElementById(“post_错误”).innerHTML=“标题不能超过40个字符!”;
返回;
}
如果(contents.value.length<1){
document.getElementById(“post_错误”).innerHTML=“请输入一些内容!”;
返回;
}
if(window.XMLHttpRequest){
post_entry_u=new XMLHttpRequest();
}否则{
post_entry=新的ActiveXObject(“Microsoft.XMLHTTP”);
}
post\u entry_uu.onreadystatechange=函数(){
if(post_entry_uu.readyState==4&&post_entry_uu.status==200){
document.getElementById(“post\u error”).innerHTML=post\u entry\uu.responseText;
如果(输入后响应文本==“添加的输入”){
//获取_条目();
}
}
}
post_entry_uu.open(“post”,“add_entry.php”,true);
post_entry_uu.setRequestHeader('Content-type','application/x-www-form-urlencoded');
post_条目发送(“title=“+title.value+”&contents=“+contents.value+”&image=“+image.value”);
}
函数remove_entry(){
}
Add_Entry.PHP
<?php
include("../scripts/database_connx.php");
include("../scripts/functions.php");
start_secure_session();
if(logged_in($sqli_con) === false) {
header("Location: ../index.php");
exit();
}
$title = mysqli_escape_string($sqli_con, strip_tags($_POST['title']));
$contents = mysqli_escape_string($sqli_con, strip_tags($_POST['contents']));
$image = mysqli_escape_string($sqli_con, strip_tags($_POST['image']));
$poster = mysqli_escape_string($sqli_con, strip_tags($_SESSION['tridantblog_username']));
echo var_dump($image);
if($image == "") {
if($stmt = $sqli_con->prepare("INSERT INTO entries (title, contents, poster) VALUES (?, ?, ?)")) {
$stmt->bind_param("sss", $title, $contents, $poster);
$stmt->execute();
$stmt->store_result();
$stmt->fetch();
if($stmt->rows_affected > 0) {
$stmt->close();
echo "<response>Added to database!</response>";
} else {
$stmt->close();
echo "<response>Could not add entry to the database!</response>";
}
}
} else {
#Check and upload images here!
if($stmt = $sqli_con->prepare("INSERT INTO entries (title, contents, image, poster) VALUES (?, ?, ?, ?)")) {
$stmt->bind_param("sss", $title, $contents, $poster);
$stmt->execute();
$stmt->store_result();
if($stmt->rows_affected > 0) {
$stmt->close();
echo "<response>Added to database!</response>";
} else {
$stmt->close();
echo "<response>Could not add entry to the database!</response>";
}
}
}
?>
使用旧的
XmlHttpRequest
对象不支持使用AJAX上载文件。可能的解决方法包括使用文件上载控件,例如,或支持上载文件的。此外,支持HTML5文件API和XmlHttpRequest2对象的现代浏览器将允许您以本机方式实现这一点。请看一看说明如何实现这一目标的示例
例如,假设您具有以下HTML表单:
<form action="upload.cgi" method="post" enctype="multipart/form-data" onsubmit="return upload(this);">
<input type="file" name="file" />
<button type="submit">Upload file to the server</button>
</form>
你也可以看看关于HTML5表单的文章。你应该看看这篇文章:。这并不是最近的事,但我相信大部分都是适用的。您可能需要一个使用iframe或flash文件的外部插件。@MCL以及
jQuery.ajax()
方法如何帮助将文件异步上传到服务器?@DarinDimitrov在编辑问题之前,我问了我的问题。现在我看到了代码,这是一个完全不同的故事。我不会说这是一个完全不同的故事。jQuery.ajax
方法不支持上传文件。OP没有使用jQuery的事实根本不会改变这一事实。所以,即使他使用了jQuery,也不会对他有多大帮助。我不是说jQuery可以提供解决方案。我只是让Liam Potter参考jQuery文档,因为他不知道“如何使用jQuery执行ajax”。此外,我不确定文件的原始位置在哪里,例如JSON-P数据可以很好地检索并发布回其他地方。他的代码澄清了这一点。不支持使用AJAX上传文件what@Musa,我更新了我的答案,使之更加精确:使用旧的XmlHttpRequest对象不支持使用AJAX上载文件。我还举例说明了如何使用内置在现代浏览器中的新XHR2对象实现这一点。
function upload(formElement) {
var xhr = new XMLHttpRequest();
xhr.open(formElement.method, formElement.action);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
// Handle response.
alert(xhr.responseText); // handle response.
}
};
xhr.send(new FormData(formElement));
return false;
}