Laravel表单-显示文件上传进度,然后在上传完成时重定向到查看


Laravel form - show file upload progress, then redirect to view when uploading finishes

我有一个成功上传文件的laravel应用程序。我需要在文件上传时显示进度条,因为用户可能会上传视频,然后在上传完成时重定向到某个视图。我已经尝试了一些解决方案,实际上是有效的,但不是我的口味。例如,我尝试过DropZone,但我只上传一个文件,我不需要拖放功能。解决方案的工作和接近我想要的是使用jquery。表单插件。但问题是,我仍然希望表单提交和重定向到下一页时,文件上传完成。意思是,当上传完成后,我仍然希望执行下一行

return redirect()->action('PostController@edit', $post->PostID);

这是我的Controller (PostController)

public function attachstore(Request $request, $PostID)
{
$post = Post::findOrFail($PostID);
if ($request->hasFile('TextFile')) {
    $TextName = "";
    $now = 'DateTime::createFromFormat('U.u', microtime(true));
    $nowStr = $now->format("m-d-Y-H-i-s-u");
    $TextName =  $nowStr . '.' . $request->file('TextFile')->getClientOriginalExtension();
    $request->file('TextFile')->move(base_path() . '/public/textuploads/', $TextName);
    $post->PostText = $TextName;  
}  

if ($request->hasFile('VideoFile')) {
    $VideoName = "";
    $now = 'DateTime::createFromFormat('U.u', microtime(true));
    $nowStr = $now->format("m-d-Y-H-i-s-u");
    $VideoName =  $nowStr . '.' . $request->file('VideoFile')->getClientOriginalExtension();
    $request->file('VideoFile')->move(base_path() . '/public/videouploads/', $VideoName);
    $post->PostVideo = $VideoName; 
}  
$post->Save();
return redirect()->action('PostController@edit', $post->PostID);

}

这是我的视图(attach.blade.php)

   {!! Form::open(array('route' => array('posts.attachstore', $post->PostID), 'files' => true)) !!}
   <div class="col-md-12">    
    <div class="form-group">     
      {!! Form::file('TextFile', null, array('class'=>'form-control')) !!}
    </div>
  </div>
  <div class="col-md-12">    
    <div class="form-group">     
      {!! Form::file('VideoFile', null, array('class'=>'form-control')) !!}
    </div>
  </div>
  {!!Form::button('Next', array('type' => 'submit', 'class' => 'btn btn-primary finish col-md-4'))  !!}
  {!! Form::close() !!}

我不知道这是否可以在不使用ajax的情况下完成,但我更喜欢使用ajax只报告服务器收到或客户端发送了多少文件的解决方案。我不想使用ajax上传文件,只需要估计传输文件的大小。

我建议使用专门的库来实现这一点,而不是重写所有的代码。

尝试这些,或者如果你谷歌其他库并使用合适的库,你会得到更多。

http://www.dropzonejs.com/

https://github.com/zimt28/laravel-jquery-file-upload

我使用blueimp fileupoad来实现。

他们有一个很好的例子,多个文件上传的代码与上传进度。你可以很容易地在javascript中添加重定向作为回调。