Laravel观点:
<form enctype="multipart/form-data" method="post" name="imagesform" id="imagesform" >
<input type="hidden" name="_token" value="{{ csrf_token()}}">
<div>
<label id="show" for="files" class="button">Upload photo</label>
<input id="files" name="images" style="visibility:hidden;" type="file">
</div>
<button type="submit" class="save" id="saveImage" style="border-radius: 8px; padding: 5px 15px;">SAVE</button>
</form>
Run Code Online (Sandbox Code Playgroud)
这是我上传图像的代码(它发生在我的引导程序模型中).当我上传图像并单击提交按钮时,图像应该插入到数据库中,并且应该检索并显示在视图页面上.
Ajax代码:
$("#saveImage").click(function(e){
// var formdata=new FormData($("#imagesform")[0]);
//alert(formdata);
$.ajaxSetup({
headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') }
});
$.ajax({
url: 'saveImages/{{$dataId}}',
type: 'POST',
data:new FormData($("#imagesform")),
success: function (data) {
alert(data)
},
cache: false,
processData: false
});
return false;
});
Run Code Online (Sandbox Code Playgroud)
这是ajax代码,我试过了.但是formdata的警告显示[object FormData]和浏览器控制台显示Method not allowed exception
Laravel路线:
Route::post('saveImages/{dataId}',['as' …Run Code Online (Sandbox Code Playgroud) 控制器功能:
public function addImages(Request $request,$imagesProductId)
{
$product = Product::create($request->all());
$filenames = array();
if ($request->images == '') {
return Redirect::back()->withErrors(['msg', 'The Message']);
}
if () {
// also need to validate on the extension and resolution of images
// (ie., if the validation fails the enum value will be "QCFailed")
} else {
foreach ($request->images as $photo) {
$filename = substr($photo->store('public/uploadedImages'), 22);
$filenames[] = asset('storage/uploadedImages/'.$filename);
ProductsPhoto::create([
'product_id' => $product->id,
'productId' => $imagesProductId,
'nonliveStatus' =>"QCVerified",
'filename' => $filename
]);
}
// echo('nonliveStatus'); …Run Code Online (Sandbox Code Playgroud) 控制器功能:
<?php
public function addImages(Request $request, $imagesProductId) {
$product = Product::create($request->all());
$filenames = array();
if (empty($request->images)) {
$message = "error";
return Redirect::back()->with('message', $message);
}
$rules = [
'images' => 'mimes:jpeg,jpg,png' // allowed MIMEs
// size in pixels
];
$validator = Validator::make($request->all(), $rules);
$result = $validator->fails() ? 'QCVerified' : 'QCFailed';
foreach ($request->images as $photo) {
// echo($result);
$filename = $photo->store('public/uploadedImages');
$filename = substr($filename, 22);
$filenames[] = asset('storage/uploadedImages/' . $filename);
ProductsPhoto::create([
'nonliveStatus' => $result,
'product_id' => $product->id,
'productId' => $imagesProductId,
'filename' => …Run Code Online (Sandbox Code Playgroud)