Laravel uses ajax to submit this form. How should the controller code be written?
phpcn_u1582
phpcn_u1582 2017-05-16 16:54:07
0
2
527

Use ajax to submit a set of forms, which contains an image.
Originally, it could be successfully saved to the database when submitted without ajax.
However, because the image needs to be compressed before uploading, it was changed to ajax submission, and the ajax request was successful. Sent, but cannot be saved to the database,
I think it may be that the code in the controller needs to be modified, but I don’t know how to change it. The code is below.
ps: The localResizeIMG plug-in is used to compress images. https://github.com/think2011/localResizeIMG

The following is the view of submitting the form using ajax:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <link href="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.bootcss.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">
</head>
<body>

<p class="container">
    <fieldset class="form-group">
        <label for="title">标题</label>
        <input type="text" class="form-control" id="title" placeholder="">
    </fieldset>
    <fieldset class="form-group">
        <label for="content">内容</label>
        <textarea class="form-control" id="content" rows="3"></textarea>
    </fieldset>
    <fieldset class="form-group">
        <label for="photo">图片</label>
        <input type="file" name="file" accept="image/*" class="form-control-file" id="photo">
        <img id="preview"/>
    </fieldset>
    <a id="submit" class="btn btn-primary">提交</a>
</p>

<script src="https://cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/tether/1.1.1/js/tether.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js"
        integrity="sha384-vZ2WRJMwsjRMW/8U7i6PWi6AlO1L79snBrmgiDpgIWJ82z8eA5lenwvxbMV1PAh7"
        crossorigin="anonymous"></script>

//用了localResizeIMG插件,用于上传图片前先压缩。https://github.com/think2011/localResizeIMG
<script src="js/dist/lrz.all.bundle.js"></script>
<script>
    $(function () {
        var $preview = $('#preview');
        var formData = null;

        $('#photo').on('change', function () {
            lrz(this.files[0], {
                width: 800
            }).then(function (rst) {
                $preview.attr('src', rst.base64);
                rst.formData.append('fileLen', rst.fileLen);
            });
        });

        $("#submit").click(function (rst) {
            $.ajax({
                type: "POST",
                url: "article/",
                dataType: 'json',
                cache: false,
                data: {
                    title: $("#title").val(),
                    content: $("#content").val(),
                    photo: rst.formData
                }
            }).done(function (msg) {
                alert("done: " + msg);
            }).fail(function (jqXHR, textStatus) {
                alert("fail: " + textStatus);
            });
        });
    });
</script>
</body>
</html>


The following is the original controller when ajax was not used. It can be successfully saved to the database. Now I don’t know how to change it.

       public function store(Requests\StoreArticleRequest $request)
    {
        $article = new Article($request->except('photo'));
        $article -> user_id = \Auth::id();

        $file = $request->file('photo');
        $destinationPath = 'uploads/';
        $extension = $file->getClientOriginalExtension();
        $fileName = \Auth::user()->id . '_' . time() . '.' . $extension;
        $file->move($destinationPath, $fileName);
        $article -> photo = '/'.$destinationPath.$fileName;

        $article->save();

        return redirect()->action('ArticleController@show', ['id' => $article->id]);
    }

The following is StoreArticleRequest

   public function rules()
    {
        return [
            'title'=>'required',
            'content'=>'required',
            'photo'=>'image'
        ];
    }
phpcn_u1582
phpcn_u1582

reply all(2)
巴扎黑

save 前你先 dd$article Take a look at the data

曾经蜡笔没有小新

Don’t forget csrf and ajax when submitting.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template