使用jquery和ajax发送接收文件

时间:2012-02-09 19:56:54

标签: php ajax jquery

您好我正在尝试与基于Java的自定义服务器之间传输文件。服务器使用outputstream来发送和接收文件,它接收一个字节数组。是否有类似的方法将文件拆分为带有jquery的字节数组并通过PHP脚本在PHP脚本中发送?

谢谢!

1 个答案:

答案 0 :(得分:0)

使用jQuery Ajax File Upload plugin。它对我有用!

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;
}