This question already has answers here:
How can I upload files asynchronously?

(34个答案)


7年前关闭。




我想使用ajax将pdf文件发送到服务器,但是找不到任何示例或代码

这个问题。我该如何解决?请帮助我

最佳答案

有一个很好的教程http://www.phpletter.com/DOWNLOAD/

阅读并了解它会为您提供帮助。

无论如何不是我的代码,但似乎是个好方法。

function ajaxFileUpload(){
    //starting setting some animation when the ajax starts and completes
    $("#loading")
    .ajaxStart(function(){
        $(this).show();
    })
    .ajaxComplete(function(){
        $(this).hide();
    });

    /*
        prepareing ajax file upload
        url: the url of script file handling the uploaded files
                    fileElementId: the file type of input element id and it will be the index of  $_FILES Array()
        dataType: it support json, xml
        secureuri:use secure protocol
        success: call back function when the ajax complete
        error: callback function when the ajax failed

            */
    $.ajaxFileUpload
    (
        {
            url:'doajaxfileupload.php',
            secureuri:false,
            fileElementId:'fileToUpload',
            dataType: 'json',
            success: function (data, status)
            {
                if(typeof(data.error) != 'undefined')
                {
                    if(data.error != '')
                    {
                        alert(data.error);
                    }else
                    {
                        alert(data.msg);
                    }
                }
            },
            error: function (data, status, e)
            {
                alert(e);
            }
        }
    )

    return false;

}

08-03 12:29