【问题标题】:PHP fileupload using jquery,ajax & json使用 jquery、ajax 和 json 上传 PHP 文件
【发布时间】:2016-09-01 05:37:03
【问题描述】:

我正在尝试将文件上传到服务器,但我无法这样做,因为文件名包含 C:\fakepath\untitled1.doc 以下是我的 jquery,

$(document).ready(function()
{
    $("#save").click(function()
        {
            var id = $("#id").val();
            //alert(id);
            if(id =="")
                {
                    var method  =   "Add";
                    id = "";
                }
            else
                {
                    var method  = "Edit";
                    id = id;
                }
            var title = $("#title").val();
            //alert(title);
            var fname = $("#fname").val();
            var lname = $("#lname").val();
            var day = $("#day").val();
            var month = $("#month").val();
            var year = $("#year").val();
            var city = $("#city").val();
            var addr = $("#addr").val();
            var tel = $("#telno").val();
            var email = $("#email").val();
            var exp = $("#exp").val();
            var reg = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
            var filename = $("#resume").val();
            if(Validate(title,fname,lname,day,month,year,city,addr,tel,email,exp,reg,filename))
                {
                    $.ajax(
                        {
                                url         :   "addResumeAjax.php",
                                type        :   "POST",
                                dataType    :   "json",
                                data        :   {
                                                    method  :   method,
                                                    id      :   id,
                                                    title   :   title,
                                                    fname   :   fname,
                                                    lname   :   lname,
                                                    day     :   day,
                                                    month   :   month,
                                                    year    :   year,
                                                    city    :   city,
                                                    addr    :   addr,
                                                    tel     :   tel,
                                                    email   :   email,
                                                    exp     :   exp,
                                                    filename:   filename
                                                },
                            ContentType     :   "aplication/json",
                            success         :   function(response)
                                                    {
                                                        $("#msg").html(response.message);
                                                        cityBind(response.citydata,response.city);
                                                },
                            error           :   function(err)
                                                    {
                                                        alert(JSON.stringify(err));
                                                    }
                        }

                    )
                }           
        }
    );
}
);

请指导我在哪里犯错。任何帮助都会得到帮助。

【问题讨论】:

  • 在你的 jQuery 中使用 serialize() 函数。它会将您的所有数据传送到您的 php 页面。并确保您在form 标签中声明了enctype="multipart/form-data"
  • 使用 serialize() 函数后,它会将除文件名之外的所有数据传送到 php 文件中。我使用警报框检查数据

标签: php json ajax


【解决方案1】:

试试这个:

JS:

$('#upload').on('click', function() {
        var file_data = $('#pic').prop('files')[0];
        var form_data = new FormData();
        form_data.append('file', file_data);   // append more data in the same way

        $.ajax({
                url         : 'upload.php',     // point to server-side PHP script 
                dataType    : 'text',           // what to expect back from the PHP script, if anything
                cache       : false,
                contentType : false,
                processData : false,
                data        : form_data,                         
                type        : 'post',
                success     : function(output){
                    alert(output);              // display response from the PHP script, if any
                }
         });
         $('#pic').val('');                     /* Clear the file container */
    });

PHP:

<?php
    if ( $_FILES['file']['error'] > 0 ){
        echo 'Error: ' . $_FILES['file']['error'] . '<br>';
    }
    else {
        if(move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']))
        {
            echo "File Uploaded Successfully";
        }
    }

?>

这对我来说很好。

【讨论】:

  • 它会抛出类似“myFileJqry.js:134 Uncaught TypeError: formdata.append is not a function”的异常
  • 我可以使用上面的代码上传文件,但找不到其他表单字段数据,你能帮我吗?...提前谢谢。
  • 使用 $_REQUEST['filed_name'] 获取任意字段的值并使用。
【解决方案2】:

查看此链接,希望对您有所帮助。

https://abandon.ie/notebook/simple-file-uploads-using-jquery-ajax

【讨论】:

  • Uncaught ReferenceError: files is not defined
  • 您使用的是哪个版本的 jquery ?你给我看你的html文件吗?
  • jQuery v1.11.0 | (c) 2005, 2014 jQuery Foundation, Inc. | jquery.org/license
  • 我可以在哪里粘贴我的 HTML,我是这个网站的新手,我不知道太多选择。
  • 我粘贴的完成标题是“kiran html for fileupload”链接同样是pastebin.com/S80PKMJZ
猜你喜欢
  • 2017-02-15
  • 1970-01-01
  • 2010-12-29
  • 2015-05-11
  • 2017-08-25
  • 2014-05-07
相关资源
最近更新 更多