Laravel LiveWire上传文件从路径创建对象并保存到S3

发布于 2025-02-09 21:27:56 字数 1543 浏览 1 评论 0原文

我将文件上传到文件上传组件,然后将临时文件路径传递到事件中。在父组件中,我需要将文件保存到S3。

我需要将路径或文件对象或某些内容传递给父组件,然后保存它,但我似乎无法使其工作。

我已经尝试通过文件对象发送以及uploadedfile对象,我的最新迭代是尝试使用file> file对象,并且我会收到以下错误:

无法解决的依赖关系解决[参数#0 [< quield>字符串$ path]]]在类Symfony \ component \ httpfoundation \ file \ file \ file中,

因此在我的子组件中,我有此代码:

public function updatedFile()
{
    $fileObj = new File($this->file->path());
    $this->emitUp('fileUploaded', $fileObj);
}

在我的父零件中,我正在侦听file> fileuploaded everts,哪个调用保存方法:

    public function save(File $uploadedFile)
    {

        if ($path = Storage::putFileAs(env('APP_ENV') . '/statements', $uploadedFile->name, 's3')) {
            $this->statement = new Statement([
                'location_id'   => $this->location->id,
                'file_name'     => $uploadedFile->name,
                'path'          => $path,
                'uploaded_by'   => Auth::user()->id,

            ]);

            $this->statement->save();
        }
    }

我还尝试使用$ uploadedfile- storeas(),并且得到了相同的结果。似乎$ uploadedFile对象不是正确的类型。我不知道我是否需要一个存储对象还是什么,并且我似乎在文档中找不到一个好的答案。

在LiveWire组件中上传文件后,我有可用的路径是LiveWire将文件保存到本地存储中的临时文件名。我也需要原始文件名,就像将其保存到数据库时上传的内容一样。

如果我删除save()方法上的类型提示,我会得到尝试在数组上读取属性“名称”。为什么$ uploadedfile一个数组而不是对象?我猜如果我删除了类型提示,它就会以数组的形式发送。我不知道..

I'm uploading a file to a file upload component, and then passing the temporary file path to the parent component with an event. In the parent component I need to save the file to s3.

I need to pass the path or a file object or something back to the parent component, and then save it, but I can't seem to get it to work.

I've tried sending over a File object, as well as an UploadedFile object, my latest iteration is to try with a File object, and I'm getting the following error:

Unresolvable dependency resolving [Parameter #0 [ <required> string $path ]] in class Symfony\Component\HttpFoundation\File\File

So in my child component I have this code:

public function updatedFile()
{
    $fileObj = new File($this->file->path());
    $this->emitUp('fileUploaded', $fileObj);
}

In my parent component I'm listening for the fileUploaded event, which calls the save method:

    public function save(File $uploadedFile)
    {

        if ($path = Storage::putFileAs(env('APP_ENV') . '/statements', $uploadedFile->name, 's3')) {
            $this->statement = new Statement([
                'location_id'   => $this->location->id,
                'file_name'     => $uploadedFile->name,
                'path'          => $path,
                'uploaded_by'   => Auth::user()->id,

            ]);

            $this->statement->save();
        }
    }

I've also tried using $uploadedFile->storeAs() and I get the same result. It seems like the $uploadedFile object is not the right type. I don't know if I need a Storage object or what and I can't seem to find a good answer in the docs.

The path I have available after uploading the file in my livewire component is the temporary file name that livewire saves the file as in local storage. I also need the original file name as well, like what was uploaded as I'm saving that to the database.

If I remove the type hint on the save() method I get Attempt to read property "name" on array. Why is $uploadedFile an array and not an object? I guess if I remove the type hint it just gets sent over as an array. I dunno..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烙印 2025-02-16 21:27:56

这是我想到的解决方案:

儿童组成部分:
公共功能UpdatedFile()
{
$ this-&gt; [[[
'file'=&gt; '必需|最大:12288'
);

        $this->emitUp('fileUploaded', [$this->file->path(), $this->file->getClientOriginalName()]);
    }

父组件:

public function save($uploadedFile)
    {
        if ($path = Storage::disk('s3')->put(env('APP_ENV') . '/statements/' . $uploadedFile[1], file_get_contents($uploadedFile[0]))) {
            $this->statement = new Statement([
                'location_id'   => $this->location->id,
                'file_name'     => $uploadedFile[1],
                'path'          => $path,
                'uploaded_by'   => Auth::user()->id,

            ]);

            $this->statement->save();
        }

    }

Here's the solution I came up with:

child component:
public function updatedFile()
{
$this->validate([
'file' => 'required|max:12288'
]);

        $this->emitUp('fileUploaded', [$this->file->path(), $this->file->getClientOriginalName()]);
    }

parent component:

public function save($uploadedFile)
    {
        if ($path = Storage::disk('s3')->put(env('APP_ENV') . '/statements/' . $uploadedFile[1], file_get_contents($uploadedFile[0]))) {
            $this->statement = new Statement([
                'location_id'   => $this->location->id,
                'file_name'     => $uploadedFile[1],
                'path'          => $path,
                'uploaded_by'   => Auth::user()->id,

            ]);

            $this->statement->save();
        }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文