我遇到一个问题,上传文件暂停一段时间(对于大尺寸(20M +)的文件)。它按预期在较小的文件上工作。我真的不知道这个的原因。 这是我的视图,其中包含上传方法(使用simpleUpload插件):
<html>
<head>
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>upload</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="/js/xhr2.js"></script>
<script type="text/javascript">
$(function(){
$('#upload').on("click",function(){
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$("#file").upload("localhost:8000/upload",function(data){
console.log(data);
},$("#prog"));
});
});
</script>
</head>
<body>
<input type="file" id="file" name="file">
<input type="button" id="upload" value="upload">
<br>
<progress value="100" max="100" min="0" id="prog" style="display: block;width:400px">
</body>
</html>
这是我的控制器:
<?php
namespace App\Http\Controllers;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class UploadsController extends Controller
{
public function getUpload(){
return view('upload');
}
public function postUpload(request $request){
// dd(\request()->all());
$time = Carbon::now();
if ($request->hasFile('file')) {
$file = $request->file('file');
$extension = $file->getClientOriginalExtension();
$upload_success = $file->storeAs('public',$file->getClientOriginalName());
if ($upload_success) {
return response()->json($upload_success, 200);
}
else {
return response()->json('error', 400);
}
}
return response()->json('no file to upload', 400);
}
}
最后我的路线:
Route::get('/upload','UploadsController@getUpload');
Route::post('/upload','UploadsController@postUpload');
感谢您的帮助。