【问题标题】:Send receive files with jquery and ajax使用 jquery 和 ajax 发送接收文件
【发布时间】:2014-05-26 17:41:48
【问题描述】:

您好,我正在尝试在基于 java 的自定义服务器之间传输文件。服务器使用输出流来发送和接收文件,它接受一个字节数组。有没有类似的方法用 jquery 将文件拆分成字节数组并在 php 脚本中通过 TCP 套接字发送?

谢谢!

【问题讨论】:

    标签: php ajax jquery


    【解决方案1】:

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

    【讨论】:

    • 感谢您的快速回复!。但是 php 脚本如何获取您发送给它的文件并将其转换为字节数组并通过 TCP 套接字发送? Tcp 套接字只接受缓冲字符串。
    • 文件将通过 AJAX 调用上传到网络服务器。在您的 PHP 代码中,您可以从 $_FILES 数组中检索文件。 $_FILES['fileToUpload'] 更具体,因为 html 文件输入元素的名称是 'fileToUpload'。
    • 阅读更多关于 PHP $_FILES 数组及其用法click here
    • 我理解正确吗?这样 jquery 脚本将文件下载到 web 服务器上,php 脚本读取它,将其转换为字节数组并发送到我们的自定义服务器?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    相关资源
    最近更新 更多