【问题标题】:how to upload the file in local public disk of laravel from vue js app?如何从 vue js 应用程序上传 laravel 本地公共磁盘中的文件?
【发布时间】:2021-01-29 00:20:32
【问题描述】:

嗨,我想从 vue js 应用程序将文件上传到 laravel 的本地公共目录,并且只在提交表单时将 唯一名称 发送到 mysql。但不知道如何用js做到这一点

// template tag
<input type="file" @change="uploadFile" >


// methods on Vue Js app


methods: {
uploadFile(){
 // logic to upload the file in local public disk and only send the unique name to mysql 
}
}

【问题讨论】:

    标签: javascript laravel file vue.js asyncfileupload


    【解决方案1】:

    我认为您仍然应该为此创建一个 API。并通过api上传

    我这里有一个代码,也许它可以提供帮助。只需将其添加到您的 laravel 控制器中即可。

    $user = User::find($request->user_id);
            if ($request->file('profile_image') !== null) {
                $imageProfile = $request->file('profile_image');
                $imageProfileSanitizedName = time() . $imageProfile->getClientOriginalName();
                $imageProfile->move("images/", $imageProfileSanitizedName);
                $user->image = $imageProfileSanitizedName;
            }
            $user->save();
    

    这个功能 $imageProfile-&gt;move("images/", $imageProfileSanitizedName); 将移至 你的 laravel 项目的 public/images 目录。

    我添加了time() 函数,以便图像文件是唯一的。

    不要忘记在你的 vuejs 方法中使用 axios 中的 formData。

    【讨论】:

    • 感谢您的帮助,我知道我只能像您解释的那样在服务器端代码中移动文件
    猜你喜欢
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 2023-03-13
    • 2016-05-17
    • 2018-04-04
    • 2017-09-02
    相关资源
    最近更新 更多