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,它允许您实现假设客户端浏览器支持它。

更多相关文章

  1. 使用jquery tablesorter插件,有一个选中项目的下拉框
  2. 非常好用的JQuery自动补全插件bigautocomplete
  3. 查询下拉框只显示年份(jquery年份插件)
  4. Jquery+Aajax 批量上传
  5. jquery省市二级联动插件
  6. jquery插件——仿新浪微博限制输入字数的textarea
  7. jQuery插件实现网页底部自动加载-类似新浪微博
  8. Bootstrap Multiselect插件使用步骤以及常见参数配置介绍
  9. jQuery基于Canvas的微信运动折线图动态曲线图插件 - wcMotionCha

随机推荐

  1. Android 疯狂足球游戏源码
  2. ProgressBar的样式及用法
  3. android删除sd卡文件
  4. Android LiveData简介(一)
  5. 编译Windows版Android(安卓)Emulator(Cup
  6. Android 使用SeekBar 变更屏幕亮度和声音
  7. android 制作自定义标题栏
  8. android:scaleType的使用
  9. Unity 对接 Android 打包编译 bug 总结之
  10. Android生命周期