小编sur*_*amy的帖子

如何使用laravel中的ajax post请求将图像插入到db中?

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)

php ajax jquery twitter-bootstrap laravel-5.4

11
推荐指数
1
解决办法
1491
查看次数

如何验证在laravel中插入的图像数组并需要根据验证插入枚举值?

控制器功能:

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 arrays validation enums laravel-5.4

9
推荐指数
1
解决办法
367
查看次数

如何从laravel中插入的图像数组中获取单个图像?

控制器功能:

<?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)

php arrays image laravel-5.4

7
推荐指数
1
解决办法
741
查看次数

标签 统计

laravel-5.4 ×3

php ×3

arrays ×2

ajax ×1

enums ×1

image ×1

jquery ×1

twitter-bootstrap ×1

validation ×1