Let me know if anyone know what is the issue with this code.
如果有人知道这段代码的问题,请告诉我。
Basically i want to upload a file using jQuery
基本上我想用jQuery上传文件
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(event) {
$('#form1').submit(function(event) {
event.preventDefault();
$.post('post.php',function(data){
$('#result').html(data);
});
});
});
</script>
</head>
<body>
<form id="form1">
<h3>Please input the XML:</h3>
<input id="file" type="file" name="file" /><br/>
<input id="submit" type="submit" value="Upload File"/>
</form>
<div id="result">call back result will appear here</div>
</body>
</html>
and my php 'post.php'
和我的php'post.php'
<?php
echo $file['tmp_name'];
?>
Uploaded File name is not returned back. Issue is i couldn't access the uploaded file.
上传的文件名不会返回。问题是我无法访问上传的文件。
Thanks in advance! Shiv
提前致谢!希夫
6 个解决方案
#1
17
Basically i want to upload a file using jQuery
基本上我想用jQuery上传文件
You cannot upload files using AJAX. You could use the jquery.form plugin which uses a hidden iframe:
您无法使用AJAX上传文件。您可以使用jquery.form插件,该插件使用隐藏的iframe:
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script type="text/javascript">
$(document).ready(function(event) {
$('#form1').ajaxForm(function(data) {
$('#result').html(data);
});
});
</script>
</head>
<body>
<form id="form1" action="post.php" method="post" enctype="multipart/form-data">
<h3>Please input the XML:</h3>
<input id="file" type="file" name="file" /><br/>
<input id="submit" type="submit" value="Upload File"/>
</form>
<div id="result">call back result will appear here</div>
</body>
</html>
Also notice the enctype="multipart/form-data"
on the form.
另请注意表单上的enctype =“multipart / form-data”。
Another possibility is to use the HTML5 File API which allows you to achieve that assuming the client browser supports it.
另一种可能性是使用HTML5文件API,它允许您实现假设客户端浏览器支持它。
#2
7
It's not possible to upload files with jQuery $.post, neverthless, with the file API and XMLHttpRequest, it's perfectly possible to upload a file in AJAX, and you can even know how much data have been uploaded yet…
使用jQuery $ .post上传文件是不可能的,无论如何,使用文件API和XMLHttpRequest,完全可以在AJAX中上传文件,你甚至可以知道已经上传了多少数据......
$('input').change(function()
{
var fileInput = document.querySelector('#file');
var xhr = new XMLHttpRequest();
xhr.open('POST', '/upload/');
xhr.upload.onprogress = function(e)
{
/*
* values that indicate the progression
* e.loaded
* e.total
*/
};
xhr.onload = function()
{
alert('upload complete');
};
// upload success
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0))
{
// if your server sends a message on upload sucess,
// get it with xhr.responseText
alert(xhr.responseText);
}
var form = new FormData();
form.append('title', this.files[0].name);
form.append('pict', fileInput.files[0]);
xhr.send(form);
}
#3
5
No no no, you should use a jQuery form plugin for async uploading files. You can't upload file with jQuery $.post method. The file will be uploaded with hidden iframe
不,不,你应该使用jQuery表单插件来异步上传文件。您无法使用jQuery $ .post方法上传文件。该文件将与隐藏的iframe一起上传
Another way is to use HTML5 uploading with FileAPI/BlobApi
另一种方法是使用FileAPI / BlobApi上传HTML5
#4
0
Try uploading with an iframe because you can't send a file with $.post method.
尝试使用iframe上传,因为您无法使用$ .post方法发送文件。
#5
0
Your upload.php has some error.
你的upload.php有一些错误。
you should change your this part.
你应该改变这一部分。
echo $file['tmp_name'];
to:-
echo $_FILES['file']['tmp_name'];
#6
-1
You could also try jquery uploadify - http://www.uploadify.com/
您也可以尝试jquery uploadify - http://www.uploadify.com/
#1
17
Basically i want to upload a file using jQuery
基本上我想用jQuery上传文件
You cannot upload files using AJAX. You could use the jquery.form plugin which uses a hidden iframe:
您无法使用AJAX上传文件。您可以使用jquery.form插件,该插件使用隐藏的iframe:
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script type="text/javascript">
$(document).ready(function(event) {
$('#form1').ajaxForm(function(data) {
$('#result').html(data);
});
});
</script>
</head>
<body>
<form id="form1" action="post.php" method="post" enctype="multipart/form-data">
<h3>Please input the XML:</h3>
<input id="file" type="file" name="file" /><br/>
<input id="submit" type="submit" value="Upload File"/>
</form>
<div id="result">call back result will appear here</div>
</body>
</html>
Also notice the enctype="multipart/form-data"
on the form.
另请注意表单上的enctype =“multipart / form-data”。
Another possibility is to use the HTML5 File API which allows you to achieve that assuming the client browser supports it.
另一种可能性是使用HTML5文件API,它允许您实现假设客户端浏览器支持它。
#2
7
It's not possible to upload files with jQuery $.post, neverthless, with the file API and XMLHttpRequest, it's perfectly possible to upload a file in AJAX, and you can even know how much data have been uploaded yet…
使用jQuery $ .post上传文件是不可能的,无论如何,使用文件API和XMLHttpRequest,完全可以在AJAX中上传文件,你甚至可以知道已经上传了多少数据......
$('input').change(function()
{
var fileInput = document.querySelector('#file');
var xhr = new XMLHttpRequest();
xhr.open('POST', '/upload/');
xhr.upload.onprogress = function(e)
{
/*
* values that indicate the progression
* e.loaded
* e.total
*/
};
xhr.onload = function()
{
alert('upload complete');
};
// upload success
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0))
{
// if your server sends a message on upload sucess,
// get it with xhr.responseText
alert(xhr.responseText);
}
var form = new FormData();
form.append('title', this.files[0].name);
form.append('pict', fileInput.files[0]);
xhr.send(form);
}
#3
5
No no no, you should use a jQuery form plugin for async uploading files. You can't upload file with jQuery $.post method. The file will be uploaded with hidden iframe
不,不,你应该使用jQuery表单插件来异步上传文件。您无法使用jQuery $ .post方法上传文件。该文件将与隐藏的iframe一起上传
Another way is to use HTML5 uploading with FileAPI/BlobApi
另一种方法是使用FileAPI / BlobApi上传HTML5
#4
0
Try uploading with an iframe because you can't send a file with $.post method.
尝试使用iframe上传,因为您无法使用$ .post方法发送文件。
#5
0
Your upload.php has some error.
你的upload.php有一些错误。
you should change your this part.
你应该改变这一部分。
echo $file['tmp_name'];
to:-
echo $_FILES['file']['tmp_name'];
#6
-1
You could also try jquery uploadify - http://www.uploadify.com/
您也可以尝试jquery uploadify - http://www.uploadify.com/