将Flash消息附加到Laravel中的文件下载响应

时间:2018-01-31 12:44:04

标签: laravel laravel-5

在我的代码中,我将一个zip文件作为流式响应返回:

return response()->stream(function() use ($zip){
            $zip->finish();
        });

我还想返回一条状态消息,说明"您的zip下载已经开始"和响应一起,但我无法在Laravel中找到一种正确的方法。

我正在使用Laravel 5.2

2 个答案:

答案 0 :(得分:1)

尝试使用Session facade

       //don't forget to "use Session;" at the top

       return response()->stream(function() use ($zip){
            $zip->finish();
            Session::flash('message', 'Download successful');
        });

在您的视图中执行以下操作:

    @if (Session::has('message'))
        <li>{!! session('message') !!}</li>
   @endif

链接到Docs

答案 1 :(得分:-1)

public class ThreadPrintingOddEven {

volatile int status = 1;

public static void main(String[] args) {
    ThreadPrintingOddEven threadNumberPrinting = new ThreadPrintingOddEven();
    Thread t1 = new Thread(new MyThread_Odd(threadNumberPrinting), "Thread1");
    Thread t2 = new Thread(new MyThread_Even(threadNumberPrinting), "Thread2");
    t1.start();
    t2.start();

}}

class MyThread_Odd implements Runnable {
private ThreadPrintingOddEven threadNumberPrinting;
ThreadLocal<Integer> localCoutn = new ThreadLocal<Integer>();

public MyThread_Odd(ThreadPrintingOddEven threadNumberPrinting) {
    this.threadNumberPrinting = threadNumberPrinting;
}

@Override
public void run() {
    synchronized (threadNumberPrinting) {
        localCoutn.set(1);
        while (threadNumberPrinting.status < 100) {

            // while (((threadNumberPrinting.status % 2) == 0 &&
            // (threadNumberPrinting.status % 2) == 0))
            while (!(threadNumberPrinting.status == localCoutn.get())) {
                try {
                    threadNumberPrinting.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + " is Printing :" + threadNumberPrinting.status);
            threadNumberPrinting.status = threadNumberPrinting.status + 1;
            int tempLocal = localCoutn.get();
            tempLocal = tempLocal + 2;
            localCoutn.set(tempLocal);

            threadNumberPrinting.notifyAll();

        }

    }

}}

class MyThread_Even implements Runnable {
private ThreadPrintingOddEven threadNumberPrinting;
ThreadLocal<Integer> localCoutn = new ThreadLocal<Integer>();

public MyThread_Even(ThreadPrintingOddEven threadNumberPrinting) {
    this.threadNumberPrinting = threadNumberPrinting;
}

@Override
public void run() {
    synchronized (threadNumberPrinting) {
        localCoutn.set(2);
        while (threadNumberPrinting.status < 100) {
            // while((threadNumberPrinting.status%2)==1){
            while (!(threadNumberPrinting.status == localCoutn.get())) {
                try {
                    threadNumberPrinting.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + " is Printing :" + threadNumberPrinting.status);
            threadNumberPrinting.status = threadNumberPrinting.status + 1;
            int tempLocal = localCoutn.get();
            tempLocal = tempLocal + 2;
            localCoutn.set(tempLocal);

            threadNumberPrinting.notifyAll();
        }
    }

}}