drz73366 2014-11-12 11:56
浏览 134
已采纳

在Laravel中使用Dropzone上传后的成功消息

Am using Dropzone to upload files in Laravel 4.2, Below is my function which process the files after uploading.

public function postDropFiles()
{
    $file = Input::file('file');

    $destinationPath = 'uploads/'.Auth::user()->username.'/files/'.date('Y-m-d');

    $extension = File::extension($file->getClientOriginalName());
    $filename =  time().str_random(12).'.'.$extension;

    $upload_success = Input::file('file')->move($destinationPath, $filename);


    if( $upload_success ) {
        return Response::json('success', 200);//->with('uploaded','Succesiful uploaded');
    } else {
       return Response::json('error', 400);
    }

}

The codes works fine , as you can see when response is 200 (success) am trying to pass variable name called "uploaded" (Commented) so that I can display the message to the user that the files successifully uploaded.

In my View I have something like this:

@if(Session::has('uploaded'))
    <div class="notice-box"><strong>{{Session::get('uploaded')}}</strong></div>
@endif

My aim is to create a session variable after uploading and then use it to display message in View. I would appreciate any help. By the way am new to Laravel.

  • 写回答

1条回答 默认 最新

  • dongtang1966 2014-11-12 12:00
    关注

    IF USING NON AJAX REQUEST

    Add it using the Session facade;

    Session::flash('uploaded', 'Successfully uploaded.');
    

    This will put it in the session for the next request and then be deleted.

    IF AJAX REQUEST

    Pass the message value through the JSON data you are passing back to the ajax request, such as;

    Response::json(array('success' => true, 'message' => 'Successfully uploaded file.'), 200);
    

    Edit: On error as recommended by itachi, this is so the error method in your ajax will be called.

    Response::make(array('success' => false, 'message' => 'Error while uploading file.'), 400);
    

    Then you can check the success flag, and then display the subsequent message wherever you want it to display via jQuery.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分