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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动