Laravel AJAX 文件上传返回空文件值

8xiog9wr  于 2023-05-23  发布在  其他
关注(0)|答案(2)|浏览(138)

当我尝试通过 AJAX 上传文件时,它返回以下内容:

{_token: "kl7rG9Qj0ABggZdvlIo85VKwMy5ET7LS1IcumnVZ", invoice_file: {}}

请求返回了_token,所以我不确定我做错了什么。
路线:

Route::post('/invoices/recieved/upload/pdf', [App\Http\Controllers\InvoiceRecievedController::class, 'storePDFAsBase64'])->name('recieved.store.pdf');

控制器:

public function storePDFAsBase64(Request $request) {
    return response()->json($request->all());
}

HTML

<meta name="csrf-token" content="{{ csrf_token() }}" />

在页眉中添加了以上 meta标记

<form data-tab="1" id="step-1" action="{{ route('recieved.store.pdf') }}" method="POST" enctype="multipart/form-data">
                @csrf
                
                <div class="space-y-6 sm:space-y-5">
                    <div class="grid grid-cols-1 gap-4 items-start">
                        <div class="sm:col-span-1">
                            <label for="invoice_file" class="block text-sm font-medium text-gray-700 sm:mt-px sm:pt-2">
                                File<span class="text-red-500">*</span>
                            </label>
                            <div class="mt-1">
                                <input type="file" name="invoice_file" id="invoice_file" required>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="border-t border-gray-200 mt-5"></div>
                <div class="mt-5 sm:mt-6  sm:grid sm:grid-cols-2 sm:gap-3 sm:grid-flow-row-dense">
                    <button type="submit" class="next_button w-full inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 {{ Auth::user()->company->settings->backgroundColor }} text-base font-medium text-white  focus:outline-none focus:ring-2 focus:ring-offset-2 sm:col-start-2 sm:text-sm">Volgende</button>
                    <button type="button" id="hide_modal" class="mt-3 w-full inline-flex justify-center rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-base font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 sm:mt-0 sm:col-start-1 sm:text-sm">Sluiten</button>
                </div>
            </form>

AJAX Post request:

$('#step-1').submit(function(e) {
        e.preventDefault();
        let formData = new FormData(this);
        $.ajax({
            type: 'POST',
            url: `/invoices/recieved/upload/pdf`,
            data: formData,
            headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
            contentType: false,
            processData: false,
            encode: true,
            success: function(response) {
                if (response) { console.log(response); }
            },
            error: function (response) {
                console.log(response);
            }
        });
    });
nkhmeac6

nkhmeac61#

只对文件尝试这样的操作:

if($request->hasFile('invoice_file')) {
        $file = $request->file('invoice_file');

        //you also need to keep file extension as well
        $name = $file->getClientOriginalName().'.'.$file->getClientOriginalExtension();

        //using array instead of object
        $image['filePath'] = $name;
        $file->move(public_path().'/uploads/', $name);

    }
lqfhib0f

lqfhib0f2#

在离开这个单独的10个月后,我决定再次潜入,并试图解决这个问题。经过几个小时的拉扯头发和沮丧地走进走出房间,我终于找到了我的问题的答案。
当然,这段代码甚至不会返回任何文件,所以在这里查找它有点愚蠢。

return response()->json($request->all());

不过,这也是一无所获:

return response()->json($request->file('file');

我偶然发现

return $request->file('file');

返回临时路径文件。
在我发现这一点之后,我得出结论,response()->json()操作文件请求,并简单地返回一个空对象。所以10个月后我发现问题出在json响应上,文件本身的实际返回工作得非常好。
史上最烂的开发商。
结案了

相关问题