当我尝试通过ajax上传图像时,Laravel出错

时间:2016-01-06 07:14:04

标签: ajax laravel-5.2

我可以使用以下代码上传图片。但在尝试使用控制器的动作通过ajax上传图像后,我得到错误:

错误

Call to a member function move()

我的代码:

{!! Form::open(['url' => 'registerToBuyCard', 'id' => 'register_to_buy_card','style'=>'text-align:left;','enctype'=>"multipart/form-data"]) !!}

<div class="form-group">
    <label for="passport">Passport Image</label>
    {!! Form::file('passport') !!}
</div>

<button type="submit" class="btn btn-default">Submit</button>
</fieldset>
{!! Form::close() !!}

的Ajax:

$("#register_to_buy_card").submit(function (event) {
    event.preventDefault();
    var $this = $(this);
    var url = $this.attr('action');

    $.ajax({
        url: url,
        type: "POST",
        dataType: "json",
        data: $this.serialize(),
        success: function (data) {
            if (data.code == '1') {
                alert('thanks');
            } else
                apprise("<span style='font-size:11px;font-family: Tahoma'>" + data.message + "</span>");
        },
        error: function (data) {
            apprise("<span style='font-size:11px;font-family: Tahoma'>" + data.message + "</span>");
        }
    });
});

控制器操作:

public function registerToBuyCard(Request $request)
{
    $file = $request->file('passport');
    $filename = $this->upload_image($file);
}
public function upload_image($filename)
{
    $destinationPath = base_path() . '/upload/';
    if ($filename->move($destinationPath, $filename))
        return $filename;
    else
        return false;
}

1 个答案:

答案 0 :(得分:0)

解决问题:

<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8" />
<title>Cumulunimbus</title>
<script type="text/javascript">

    function Point(x, y) {
        this.x = x;
        this.y = y;
    }

    function get(obj, prop, fallback) {
        if (obj.hasOwnProperty(prop)) return obj[prop];
        return fallback;
    }

    /*
     *      Global intersection angles of two circles of the same radius
     */
    function intersect(p, q, r) {
        var dx = q.x - p.x;
        var dy = q.y - p.y;

        var len = Math.sqrt(dx*dx + dy*dy);
        var a = 0.5 * len / r;

        if (a < -1) a = -1;
        if (a > 1) a = 1;

        var phi = Math.atan2(dy, dx);
        var gamma = Math.acos(a);

        return [phi - gamma, Math.PI + phi + gamma];
    }

    /*
     *      Draw a cloud with the given options to the given context
     */
    function cloud(cx, poly, opt) {        
        var radius = get(opt, "radius", 20);
        var overlap = get(opt, "overlap", 5/6);
        var stretch = get(opt, "stretch", true);



        // Create a list of circles

        var circle = [];        
        var delta = 2 * radius * overlap;

        var prev = poly[poly.length - 1];
        for (var i = 0; i < poly.length; i++) {
            var curr = poly[i];

            var dx = curr.x - prev.x;
            var dy = curr.y - prev.y;

            var len = Math.sqrt(dx*dx + dy*dy);

            dx = dx / len;
            dy = dy / len;

            var d = delta;

            if (stretch) {
                var n = (len / delta + 0.5) | 0;

                if (n < 1) n = 1;
                d = len / n;
            }

            for (var a = 0; a + 0.1 * d < len; a += d) {
                circle.push({
                    x: prev.x + a * dx,
                    y: prev.y + a * dy,
                });
            }

            prev = curr;
        }



        // Determine intersection angles of circles

        var prev = circle[circle.length - 1];
        for (var i = 0; i < circle.length; i++) {
            var curr = circle[i];
            var angle = intersect(prev, curr, radius);

            prev.end = angle[0];
            curr.begin = angle[1];

            prev = curr;
        }



        // Draw the cloud

        cx.save();

        if (get(opt, "fill", false)) {
            cx.fillStyle = opt.fill;

            cx.beginPath();
            for (var i = 0; i < circle.length; i++) {
                var curr = circle[i];

                cx.arc(curr.x, curr.y, radius, curr.begin, curr.end);
            }
            cx.fill();
        }

        if (get(opt, "outline", false)) {
            cx.strokeStyle = opt.outline;
            cx.lineWidth = get(opt, "width", 1.0);

            var incise = Math.PI * get(opt, "incise", 15) / 180;

            for (var i = 0; i < circle.length; i++) {
                var curr = circle[i];

                cx.beginPath();
                cx.arc(curr.x, curr.y, radius,
                    curr.begin, curr.end + incise);
                cx.stroke();
            }
        }

        cx.restore();
    }

    var poly = [
        new Point(250, 50),
        new Point(450, 150),
        new Point(350, 450),
        new Point(50, 300),
    ];

    window.onload = function() {
        cv = document.getElementById("cv");
        cx = cv.getContext("2d");

        cloud(cx, poly, {
            fill: "lightblue",        // fill colour
            outline: "black",         // outline colour
            incise: 15,               // overshoot in degrees
            radius: 20,               // arc radius
            overlap: 0.8333,          // arc distance relative to radius
            stretch: false,           // should corner arcs coincide?
        });
    }

</script>
</head>

<body>
<canvas width="500" height="500" id="cv"></canvas>
</body>

</html>