块上传多个插入

时间:2017-05-08 18:10:35

标签: jquery jquery-file-upload blueimp

每次上传分块上传时,所有的块都会在数据库中多次插入

每个chunk = 1插入数据库

protected function handle_file_upload($uploaded_file, $name, $size, $type, $error, $index = null, $content_range = null)
{
    $file = parent::handle_file_upload( $uploaded_file, $name, $size, $type, $error, $index, $content_range );

    if (empty($file->error)) {

        DB::table('test')->insert([
           'name' => json_encode($file)
        ]);


    }
    return $file;
}

1 个答案:

答案 0 :(得分:0)

好的,我解决了这个问题。

为了避免在使用块上传时多次插入

protected function handle_file_upload($uploaded_file, $name, $size, $type, $error,
                                      $index = null, $content_range = null)
{
    $file = parent::handle_file_upload(
        $uploaded_file, $name, $size, $type, $error, $index, $content_range
    );


    // check if a file is needed to be chunked, 
    // if file is null, file does not need to be chunked otherwise file should be chunked upload
    if( ! is_null($content_range) )
    {
        // ex. the file is 1mb then last chunk would be 999,999
        $last_chunk = (int) $content_range[2];
        // get the filesize of the entire
        $filesize = (int) $content_range[3];

        // get the last chunk then add 1 to it and compare it to the filesize
        if( ($last_chunk + 1) === $filesize )
        {
            // db actions like insert to db or update something
        }
    } else {

        // other things
    }


    return $file;
}