PHP文件上传使用jquery后 [英] PHP file-upload using jquery post

查看:91
本文介绍了PHP文件上传使用jquery后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我知道是否有人知道这个代码是什么问题。

基本上我想用jQuery上传一个文件

 < / p> 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>请输入XML:< / h3>
< input id =filetype =filename =file/>< br />
< input id =submittype =submitvalue =Upload File/>
< / form>

< div id =result>回电结果会显示在这里< / div>

< / body>
< / html>

和我的php'post.php'

 <?php 
echo $ file ['tmp_name'];
?>

上传的文件名不会返回。问题是我无法访问上传的文件。



在此先感谢!
Shiv

解决方案


基本上我想用jQuery上传文件


您无法使用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 =form1action =post.phpmethod =postenctype =multipart / form-data>
< h3>请输入XML:< / h3>
< input id =filetype =filename =file/>< br />
< input id =submittype =submitvalue =Upload File/>
< / form>

< div id =result>回电结果会显示在这里< / div>

< / body>
< / html>

另请注意 enctype =multipart / form-data



另一种可能性是使用 HTML5 File API ,它可以让你实现假设客户端浏览器支持它。


Let me know if anyone know what is the issue with this code.

Basically i want to upload a file using 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
  echo $file['tmp_name'];
?>

Uploaded File name is not returned back. Issue is i couldn't access the uploaded file.

Thanks in advance! Shiv

解决方案

Basically i want to upload a file using jQuery

You cannot upload files using AJAX. You could use the jquery.form plugin which uses a hidden 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.

Another possibility is to use the HTML5 File API which allows you to achieve that assuming the client browser supports it.

这篇关于PHP文件上传使用jquery后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆