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

在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.

使用Dropzone在Laravel 4.2中上传文件,下面是我上传后处理文件的函数。 p >

  public function postDropFiles()
 {
 $ file = Input :: file('file'); 
 
 $ destinationPath ='uploads /'。Auth :: user  () - &gt;用户名。'/ files /'。date('Ym-d'); 
 
 $ extension = File :: extension($ file-&gt; getClientOriginalName()); 
 $ filename = time  ().str_random(12)。'。'。$ extension; 
 
 $ upload_success = Input :: file('file') - &gt; move($ destinationPath,$ filename); 
 
 
 if  ($ upload_success){
返回Response :: json('success',200); //  - &gt; with('uploaded','Succesiful uploaded'); 
} else {
 return Response :: json(  '错误',400); 
} 
 
} 
  code>  pre> 
 
 

代码工作正常,因为您可以看到响应为200(成功)时我正在尝试 传递名为“已上传”的变量名称(已评论),以便我可以向用户显示已成功上传文件的消息。 p>

在我的 查看我有这样的事情: p>

  @if(Session :: has('uploaded'))
&lt; div class =“notice-box”&gt;&lt;  strong&gt; {{Session :: get('uploaded')}}&lt; / strong&gt;&lt; / div&gt; 
 @ endif 
  code>  pre> 
 
 

我的目标是 上传后创建会话变量,然后使用它在View中显示消息。 我会感激任何帮助。 顺便说一下,我是Laravel的新手。 p> div>

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.