使用 jquery post 上传 PHP 文件

PHP file-upload using jquery post(使用 jquery post 上传 PHP 文件)
本文介绍了使用 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 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Uploading Image from android to PHP server(将图像从android上传到PHP服务器)
Sending file together with form data via ajax post(通过ajax post将文件与表单数据一起发送)
Laravel - validate file size when PHP max upload size limit is exceeded(Laravel - 超过 PHP 最大上传大小限制时验证文件大小)
How to upload file through Jquery/AJAX(如何通过 Jquery/AJAX 上传文件)
PHP: move_uploaded_file() failed to open stream: No such file or directory(PHP:move_uploaded_file() 无法打开流:没有那个文件或目录)
Creating a file progress bar in PHP(在 PHP 中创建文件进度条)