2017-05-08 48 views
0

每次一个组块上载上载,所有的块被多次插入到数据库组块上载多个在DB</p> <p>每个块= 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; 
} 

回答

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