Laravel LiveWire上传文件从路径创建对象并保存到S3
我将文件上传到文件上传组件,然后将临时文件路径传递到事件中。在父组件中,我需要将文件保存到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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我想到的解决方案:
儿童组成部分:
公共功能UpdatedFile()
{
$ this-&gt; [[[
'file'=&gt; '必需|最大:12288'
);
父组件:
Here's the solution I came up with:
child component:
public function updatedFile()
{
$this->validate([
'file' => 'required|max:12288'
]);
parent component: