【问题标题】:using html5 for file upload with ajax and jquery使用 html5 通过 ajax 和 jquery 进行文件上传
【发布时间】:2013-01-05 12:06:46
【问题描述】:

所以我正在尝试将图像与表单数据一起上传到服务器。我正在使用 FileReader API 将图像转换为数据并上传到服务器。我正在遵循类似于HTML5 uploader using AJAX Jquery 的代码。

数据在 jquery 中转换,但没有发送到服务器,也没有产生错误。

$('#formupload').on('submit', function(e){
    e.preventDefault();
    var hasError = false;
    var file = document.getElementById('file').files[0];
    var reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = shipOff;

    function shipOff(event) {
        result = new Image(); 
        result.src = event.target.result;
        var fileName = document.getElementById('file').files[0].name; 
        $.post('test.php', { data: result, name: fileName });
    }

PHP 代码

<?php
$data = $_POST['data'];
$fileName = $_POST['name'];
echo $fileName;
$fp = fopen('/uploads/'.$fileName,'w'); //Prepends timestamp to prevent overwriting
fwrite($fp, $data);
fclose($fp);
$returnData = array( "serverFile" => $fileName );
echo json_encode($returnData);
?>

是图像文件过大还是 FileReader API 造成的问题?

【问题讨论】:

    标签: javascript jquery ajax html file-upload


    【解决方案1】:

    我不确定文件上传是否适用于文件阅读器,但有一种不同的方法可以让它工作:

    var formData = new FormData($(".file_upload_form")[0]);
    $.ajax({
        url: "upload_file.php", // server script to process data (POST !!!)
        type: 'POST',
        xhr: function() { // custom xhr
            myXhr = $.ajaxSettings.xhr();
            if (myXhr.upload) { // check if upload property exists
                // for handling the progress of the upload
                myXhr.upload.addEventListener('progress', progressHandlingFunction, false); 
            }
            return myXhr;
        },
        success: function(result) {
            console.log($.ajaxSettings.xhr().upload);
            alert(result);
        },
        // Form data
        data: formData,
        //Options to tell JQuery not to process data or worry about content-type
        cache: false,
        contentType: "application/octet-stream", // Helps recognize data as bytes
        processData: false
    });
    
    function progressHandlingFunction(e) {
        if (e.lengthComputable) {
            $("#progress").text(e.loaded + " / " + e.total);
        }
    }
    

    这样你将数据发送到 PHP 文件,你可以使用$_FILES 来处理它。不幸的是,据我所知,这在 IE 中不起作用。在 IE 中可能有可用的插件使这成为可能,但我不知道其中任何一个。

    【讨论】:

    • 此代码只会将图像发送到服务器。我需要同时发送图像和其他表单数据。我能够将数据发送到服务器必须将 /uploads/ 替换为 uploads/。但是图片内容打不开
    • @de-bugged 这实际上将发送图像和数据,var formData = new FormData($('.file_upload_form')[0]); 将确保这一点。该表单可以同时包含文件上传和其他数据,并将其简单地转换为普通的$_POST 值。如果您有一个名称为 text 的文本输入,则该文本输入也可以在 $_POST 中访问。
    • @de-bugged 如果这对您有帮助,请按此答案旁边的接受按钮 :)
    • Hi Rune,试过你的方法。 formData 的内容是 append:function append() 并且没有其他表单数据。我还尝试将所有表单数据附加到 formData 并调用 post 请求。没有数据发送到服务器。有什么建议吗?
    • 您应该为此获得 + 1,000,000,000 的收益。一直在寻找一种可以全天使用的跨域解决方案,而您的解决方案是唯一可行的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 2011-06-18
    相关资源
    最近更新 更多