使用FileReader和php上传大文件 [英] Large file uploads using FileReader and php

查看:247
本文介绍了使用FileReader和php上传大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为非常大的文件(比服务器允许的更大)开发一个上传模块,其中包含进度和所有内容。请参阅下面的代码。
适用于文本文件,图像,甚至doc和pdf文件。
它崩溃了任何其他文件类型。
任何人有什么建议吗?

I am currently developing an upload module for very large files (larger than the server will ever allow) with progress and all that stuff. See the code bellow. It works for text files, images, even doc and pdf files. It crashes for any other file type. Anyone has any suggestions?

var fr = new FileReader;
chunkSize = 524288;
//chunkSize = window.maxPost;
var chunks = Math.ceil(file.size / chunkSize);
var chunk = 0;
function SendSlice() {
    var start, end;
    start = chunk * chunkSize;
    if (start > file.size) {
        start = end + 1;
    }
    end = start + (chunkSize - 1) >= file.size ? file.size : start + (chunkSize - 1);
    status = chunk == 0 ? "start" : (chunk == chunks ? "end" : "progress");
    if (status == 'start') {
            $("#upload-area").append("<p>Upload started for file " + file.name + "</p>");
    }
    fr.onload = function(e) {
        var sliceData = e.target.result;
        $.ajax({
            type : "POST",
        url : "uploader.php",
        data : {
            filename : file.name,
            status : status,
            slice : sliceData
        }
        }).success(function(data) {
            if (++chunk <= chunks) {
                SendSlice();
                var progress = (chunk / chunks) * 100;
                $("#progress-text").html(progress.toFixed(2) + "%");
                $("#progress").css({
                    width : progress + "%"
                });
            } else {
                $("#upload-area").append("<p>File " + file.name + " uploaded succesfully. Download file <a target='_blank' href='uploads/" + file.name + "'>here</a></p>");
            }
        });
    };
    fr.readAsDataURL(file.slice(start, end));
}
SendSlice();

和php代码:

 if($_POST['status'] == 'start') {
if (file_exists("uploads/" . $_POST['filename'])) {
    unlink("uploads/" . $_POST['filename']);
}
 }
 $data = explode(",", $_POST['slice']);
 $data = $data[1];
 $data = base64_decode($data);
 file_put_contents("uploads/" . $_POST['filename'], $data, FILE_APPEND);

另外,我尝试过使用readAsBinaryString,但我不知道如何处理PHP中的结果。请建议

Also, i have tried using readAsBinaryString, but i have not idea how the handle the result in PHP. Please advice

推荐答案

这只是一个黑暗中的镜头,但是看一下file.slice API( http://www.w3.org/TR/FileAPI/#dfn-slice ),它说:

This is just a shot in the dark, but looking at the file.slice API (http://www.w3.org/TR/FileAPI/#dfn-slice), it says:

切片方法返回一个新的Blob对象,其字节范围从可选的start参数到最大但不包括可选的end参数,并且具有类型属性可选的contentType参数的值。

"The slice method returns a new Blob object with bytes ranging from the optional start parameter upto but not including the optional end parameter, and with a type attribute that is the value of the optional contentType parameter."

然而,在使用它之前,你从end中减去1 - 这是否意味着你在每个块中省略1个字节(因为无论如何都不包括结束字节)?

However, you subtract 1 from "end" before using it - does that mean you leave out 1 byte at each chunk (since the end byte isn't included anyway)?

(另外,在使用它之前,你要清理$ _POST ['filename'] - 而不是有人放../ yourscript.php在那里......对吗?;)

(Also, you do sanitize $_POST['filename'] before using it - not that someone puts "../yourscript.php" in there .. right? ;) )

这篇关于使用FileReader和php上传大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆