简体   繁体   English

jQuery文件上传未上传

[英]jQuery File Upload not Uploading

This PHP code works when I use HTML <input type="file"> , but not when I use the jQuery file upload script at the very bottom. 当我使用HTML <input type="file"> ,此PHP代码有效,但在最底部使用jQuery文件上传脚本时,此PHP代码不起作用。

  1. I believe all .js & .css files are accounted for and working (tested) 我相信所有.js和.css文件都已解决并且可以正常工作(经过测试)
  2. I believe the uploadUrl: 'doc_upload.php', fileInputName: 'userfile' are correct. 我相信uploadUrl: 'doc_upload.php', fileInputName: 'userfile'是正确的。 Don't know 'cause no errors are showing. 不知道,因为没有错误显示。
  3. Using echo getcwd(); 使用echo getcwd(); , I believe I'm using correct paths ,我相信我使用的是正确的路径
  4. I'm only uploading a 1 kb .TXT file 我只上传1 kb .TXT文件
  5. I get no errors and all error reporting is on and maxed 我没有任何错误,并且所有错误报告均已启用并已最大化

One thing that is also not working, which I'm saying to possibly give a clue, is the upload & cancel buttons next to the file name are not showing all though clicking on them does seem to work. 我要说的一个可能也是不起作用的事情是,尽管单击文件名旁边的文件名旁边的“上载和取消”按钮似乎没有全部显示。

Echo'ing $_POST shows me nothing on submit, but I think that's a javascript thing; 回显$ _POST不会显示任何提交内容,但是我认为这是JavaScript的事情; not sure as I'm a beginner (3-months). 不确定,因为我是初学者(3个月)。

PHP page PHP页面

    $filename = $_FILES['userfile']['name'];
    $tmpname  = $_FILES['userfile']['tmpname'];
    $filesize = $_FILES['userfile']['size'];
    $filetype = $_FILES['userfile']['type'];

    $fp = fopen($tmpname, 'r');
    $content = fread($fp, filesize($tmpname));
    $content = addslashes($content);
    fclose($fp);

    if(!get_magic_quotes_gpc()) {

        $filename = addslashes($filename);
    }

    $filename = preg_replace('/[ ]/', '~', $filename);

    $document_folder = $_SESSION['users_id'];
    $destfile = "../_documents/".$document_folder."/".$_FILES['userfile']['name'];
    move_uploaded_file( $_FILES['userfile']['tmpname'], $destfile );

HTML page HTML页面

<link type="text/css" rel="Stylesheet" href="../jqx.base.css" />

<script type="text/javascript" src="../scripts/js/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="../scripts/jqwidgets/jqxcore.js"></script>
<script type="text/javascript" src="../scripts/jqwidgets/jqxbuttons.js"></script>
<script type="text/javascript" src="../scripts/jqwidgets/jqxfileupload.js"></script>

<script type="text/javascript">


        $(document).ready(function () {
      $('#jqxFileUpload').jqxFileUpload({ width: 300, uploadUrl: 'doc_upload_jquery.php', fileInputName: 'userfile', autoUpload: true });
        });



    $('#jqxFileUpload').on('uploadStart', function (event) {
            var fileName = event.args.file;
            $('#log').prepend('Started uploading: <strong>' + fileName + '</strong><br />');
    });



    $('#jqxFileUpload').on('remove', function (event) {
            var fileName = event.args.file;
        $('#log').prepend('Removed file: <strong>' + fileName + '</strong><br />');

    });



    $('#jqxFileUpload').on('uploadEnd', function (event) {
        var args = event.args;
        var fileName = args.file;
        var serverResponce = args.response;
    });



</script>


</head>
<body>
<div id="jqxFileUpload">
</div>
<div id="log" style="margin-top: 20px;"></div>
<br />
</body>
</html>

mdpalow - mdpalow-

You probably aren't getting any errors or output because of the if statement at the top: 由于顶部的if语句,您可能没有得到任何错误或输出:

if (isset($_POST['submit']) && $_FILES['userfile']) {

Try putting this at the top to output all of your post variables so that you can determine what post variables are actually being sent (if any): 尝试将其放在顶部以输出所有post变量,以便您可以确定实际发送的post变量(如果有):

print_r($_POST);

I have done file uploads before but not with this specific library - my guess is that its just sending mulipart data instead of POST variables. 我之前已经完成了文件上传,但是没有使用该特定库上传-我的猜测是它只是发送mulipart数据而不是POST变量。 Looking at the preview headers in the browser developer tools will also shed light as to what data is being sent. 在浏览器开发人员工具中查看预览标头也将阐明正在发送的数据。

The problem is that jQuery plugin uploads file via AJAX. 问题是jQuery插件通过AJAX上传文件。 In order to fix the issue please follow next steps: 为了解决此问题,请按照以下步骤操作:

  • Keep your HTML and PHP code in separate files. 将您的HTML和PHP代码保存在单独的文件中。
  • In new PHP file remove isset($_POST['submit']) validation as there is no submission. 在新的PHP文件中,删除isset($ _ POST ['submit'])验证,因为没有提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM