简体   繁体   English

Laravel + vue.js 文件上传放

[英]Laravel + vue.js file upload put

I've successful uploaded files with vue.js 1.0 like this (post request):我已经成功上传了vue.js 1.0这样的文件(发布请求):

store () {
            this.storingMessage = true;
            var form = new FormData();
            form.append('subject', this.message.subject);
            form.append('message', this.message.message);
            form.append('topic_id', this.message.topic_id);

            for(var key in this.message.attachment) {
                form.append('attachment[' + key + ']', this.message.attachment[key]);
            }

            MessageService.store(this, form);
        }

But when I try to update files like this (put request):但是当我尝试更新这样的文件时(放置请求):

update () {
            this.updatingMessage = true;
            var form = new FormData();
            form.append('subject', this.message.subject);
            form.append('message', this.message.message);
            form.append('slug', this.message.slug);

            for(var key in this.message.attachment) {
                form.append('attachment[' + key + ']', this.message.attachment[key]);
            }

            MessageService.update(this, form);
        }

And in my controller I dd($request->all()) result is [] .在我的控制器中,我dd($request->all())结果是[]

So why is it not working with put ????!?!?!?那么为什么它不能与put一起使用????!?!?!?

Services:服务:

store (context, form) {
        return Vue.http.post('/api/message', form)
            .then(({data}) => {
                context.success();
            }, (error) => {
                context.error(error.data);
            });
    },

    update (context, form) {
        return Vue.http.put('/api/message/' + form.get('slug'), form)
            .then(({data}) => {
                context.success();
            }, (error) => {
                context.error(error.data);
            });
    }

According to a Laracast Discussion it would seem there is a problem when using the put method and a formData object a way to avoid this is with method spoofing.根据Laracast 的讨论,使用 put 方法和 formData 对象时似乎存在问题,避免这种情况的方法是使用方法欺骗。

Try this instead, in your update method试试这个,在你的更新方法中

let data = { _method : 'PATCH' , form : form}

return Vue.http.post('/api/message/' + form.get('slug'), data)
    .then(({data}) => {
        context.success();
    }, (error) => {
        context.error(error.data);
    });

your data in your controller will be available as form in this case在这种情况下,您的控制器中的数据将以form提供

$request->form

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM