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

时间:2016-11-09 15:38:57

标签: php laravel laravel-5.1

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

返回redirect() - >动作(' 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上传文件,只是估计传输的文件大小。

2 个答案:

答案 0 :(得分:0)

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

尝试其中任何一种,或者如果你谷歌搜索其他图书馆并使用适合的任何内容,你会得到更多。

http://www.dropzonejs.com/

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

答案 1 :(得分:0)

我使用blueimp fileupoad来实现这一目标。

他们有一个great example,代码为多个文件上传和上传进度。您可以轻松地在javascript中添加重定向作为回调。