0

Can anyone please tell me, why The exportMPR returns a 302 status code with no-response? I don't understand what's wrong with the code.

FYI: Excel Package: https://laravel-excel.com/ is used
Laravel Version: 10

Controller function

public function exportMPR()
    {
        try {
            return Excel::download(new MPRExport(request('month'), request('year')), 'mpr.xlsx');
        } catch (Exception $e) {
            dd($e);
        }
    }

Web.php

Route::middleware('auth')->group(function () {

    Route::controller(MPRController::class)->name('mpr.')->prefix('mpr')->group(function (): void {
        Route::post('export-mpr', 'exportMPR')->name('exportMPR');
    });
});

ViewMPR.vue File

const downloadMPR = async () => {
    downloadMPRForm
        .transform((data) => ({
            year: data.yearMonth.year,
            month: data.yearMonth.monthIndex
        }))
        .post('export-mpr')
}

 <!-- Download MPR Form -->
            <div class="bg-white rounded-lg shadow-lg border p-5">
                <h1 class="text-gray-800 md:text-2xl font-bold mb-2">
                    Download MPR
                </h1>
                <form @submit.prevent="downloadMPR">
                    <div class="mb-6">
                        <month-picker-input :no-default="true" @change="getMonthYear"
                            v-model="downloadMPRForm.yearMonth"></month-picker-input>
                    </div>
                    <button type="submit"
                        class="text-white bg-blue-700  focus:outline-none  font-medium rounded-lg text-sm px-5 py-2.5 "
                        :disabled="downloadMPRForm.processing" :class="{ 'opacity-25': downloadMPRForm.processing }">
                        Submit
                    </button>
                </form>
            </div>

Auth Middleware

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     */
    protected function redirectTo(Request $request): ?string
    {
        return $request->expectsJson() ? null : route('login');
    }
}

I've tried various other ways such as storing it in storage and fetching it, or dumping raw data, but it always ends up with 302 redirect. I was expecting, either direct file download or raw blob data.

1 Answer 1

1

Actualy the file has been dowloaded and stored at the

storage/framework/cache/laravel-excel

if you want to download it try calling the URL in new page it will be something like :

    function download() {
    window.open(url, '_blank')
}

hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.