【问题标题】:upload file with jquery用jquery上传文件
【发布时间】:2011-10-13 11:56:51
【问题描述】:

我想用ajax上传文件

这是我的代码 php, html:

<form action="uploadVideo.php" method="POST" enctype="multipart/form-data">  
    <input type="file" name="choosefilebtn" id="choosefilebtn"  size="50" /> 
    <input type="button" class="uploadbutton" value="upload" name="uploadbtn" id="uploadbtn" />     
</form>

jquery:

$(function() {
    $('#uploadbtn').click(function() {
        var filename = $('#choosefilebtn').val();
        alert(filename);
        $.ajax({
            type: "POST",
            url: "uploadVideo.php",
            enctype: 'multipart/form-data',
            data: {
                file: filename
            },
            success: function() {
                alert("Data Uploaded: ");
            }
        });
    });
});

当我使用 sumbmit 类型作为上传按钮(不带 ajax)时,它可以工作,但使用 ajax 时它不起作用,任何人都可以帮助我, 谢谢


编辑: 添加了uploadVideo.php代码

$publish->remotehost=$ftpremotehost;
$publish->username=$ftpusername;
$publish->password=$ftppassword;
$publish->remotedir=CONSTANT_SERVERROOT.$folderName;
$publish->filename=$_FILES['choosefilebtn']['name'];
$publish->FTPLogin();
$publish->filecontent = fread( fopen($_FILES['choosefilebtn']['tmp_name'], "rb"), 
                                     $_FILES['choosefilebtn']['size']);
$publish->makedir($publish->remotedir);
$result=$publish->Publish();

【问题讨论】:

    标签: php ajax file upload


    【解决方案1】:

    标记

    <form action="processupload.php" method="post" enctype="multipart/form-data" id="MyUploadForm">
        <input name="FileInput" id="FileInput" type="file" />
        <input type="submit"  id="submit-btn" value="Upload" />
        <img src="images/ajax-loader.gif" id="loading-img" style="display:none;" alt="Please Wait"/>
    </form>
    <div id="progressbox">
        <div id="progressbar"></div>
        <div id="statustxt">0%</div>
    </div>
    <div id="output"></div>
    

    jquery

    $(document).ready(function () {
        var options = {
            target: '#output',   // target element(s) to be updated with server response 
            beforeSubmit: beforeSubmit,  // pre-submit callback 
            success: afterSuccess,  // post-submit callback 
            uploadProgress: OnProgress, //upload progress callback 
            resetForm: true        // reset the form after successful submit 
        };
    
        $('#MyUploadForm').submit(function () {
            $(this).ajaxSubmit(options);
            return false;
        });
    });
    

    【讨论】:

      【解决方案2】:

      您会注意到通过 ajax 调用发送的是文件名,而不是该文件的内容:

          $.ajax({
              //...
              data: {
                  file: filename //just a name, no file contents!
              },
              //...
          });
      

      我知道通过 ajax 发送文件数据的唯一方法是使用隐藏的 iframe 并向其提交表单

      即有

      <iframe style="display: none" id="formtarget" />
      <form action="uploadVideo.php" method="POST"  enctype="multipart/form-data"
          target="formtarget">  
          <input type="file" name="choosefilebtn" id="choosefilebtn"  size="50" /> 
          <input type="button" class="uploadbutton" value="upload" name="uploadbtn" id="uploadbtn" />     
      </form>
      

      【讨论】:

      • 代码只传递文件名,例如 C:\fakepath[myfilename.mp4]
      • 它调用脚本,脚本创建文件路径,但文件路径为空,我想是因为我的代码无法正确传递文件路径
      • @Hashem,您还以不同的名称发送参数...请参阅我的编辑。还有uploadVideo.php页面上的代码是什么
      • 感谢您的 cmets,我将 file:filename 更改为 uploadfilebtn:file name,但结果没有变化这是 php 代码:$publish->remotehost=$ftpremotehost; $publish->用户名=$ftpusername; $发布->密码=$ftp密码; $publish->remotedir=CONSTANT_SERVERROOT.$folderName; $publish->filename=$_FILES['choosefilebtn']['name']; $publish->FTPLogin(); $publish->filecontent = fread( fopen($_FILES['choosefilebtn']['tmp_name'], "rb"), $_FILES['choosefilebtn']['size']); $publish->makedir($publish->remotedir); $result=$publish->Publish();
      • 对,所以您 正在 尝试根据该代码使用文件中的数据 - 您是 fread 从中获取的。您必须使用 iframe 方法。 jQuery ajax 不能像那样发送文件内容。
      猜你喜欢
      • 2014-08-15
      • 1970-01-01
      • 2014-05-04
      • 2016-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-19
      • 2013-08-28
      相关资源
      最近更新 更多