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

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

问题描述

如果有人知道此代码有什么问题,请告诉我.

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

基本上我想使用 jQuery 上传文件

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>

和我的 php 'post.php'

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.

提前致谢!湿婆

推荐答案

基本上我想使用 jQuery 上传文件

Basically i want to upload a file using jQuery

您不能使用 AJAX 上传文件.您可以使用使用隐藏 iframe 的 jquery.form 插件:

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>

还要注意表单上的 enctype="multipart/form-data".

另一种可能性是使用 HTML5 文件 API,假设客户端浏览器支持它,您可以实现这一点.

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

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

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