javascript - Large file uploads using FileReader and php - Stack Overflow

I am currently developing an upload module for very large files (larger than the server will ever allow

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();

And the php code:

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

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

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();

And the php code:

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

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

Share Improve this question asked Feb 14, 2013 at 13:56 d4rkpr1nc3d4rkpr1nc3 1,82713 silver badges16 bronze badges 2
  • When you say 'crashes' - what error message are you actually getting? And, what is a particular file you're using to upload - give an example of the type. That should help. – Aaron Saray Commented Feb 14, 2013 at 14:25
  • yeah, sorry about that. It does not actually crash. It uploads the file, but when i try to download it, it's corupt. I've tried uploading zip, rar, cab, and even exe files. – d4rkpr1nc3 Commented Feb 14, 2013 at 14:37
Add a ment  | 

1 Answer 1

Reset to default 4

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

"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."

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)?

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745161949a4614425.html

相关推荐

  • javascript - Large file uploads using FileReader and php - Stack Overflow

    I am currently developing an upload module for very large files (larger than the server will ever allow

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信