在输入类型文件[duplicate]中设置默认值

时间:2022-05-29 17:08:23

This question already has an answer here:

这个问题已经有了答案:

Note:

注意:

The answer(s) below reflect the state of legacy browsers in 2009. Now you can actually set the value of the file input element via JavaScript in 2017.

下面的答案反映了2009年遗留浏览器的状况。现在,您可以在2017年通过JavaScript设置文件输入元素的值。

See the answer in this question for details as well as a demo:
How to set file input value programatically (i.e.: when drag-dropping files)?

有关详细信息和演示,请参见这个问题的答案:如何程序化地设置文件输入值(即。:当拖放文件)?

I know this question is duplicated but I think maybe someone can help me.

我知道这个问题是重复的,但我想也许有人能帮我。

Let me ask first the problem.

让我先问一下这个问题。

I have a form to update some fields: name, order, public, pathheader and pathhome and my question is this:

我有一个表单来更新一些字段:name, order, public, pathheader和pathhome,我的问题是:

It's possible to update the form, with the same value in pathheader and pathhome without clicking in the input type file again?

可以更新表单,在pathheader和pathhome中使用相同的值,而无需再次单击输入类型文件?

The input type file looks like here:

输入类型文件如下所示:

@if (Storage::disk('projects')->has($project->slug))
    <img src="{{ asset('/storage/projects/'.$project->slug.'/header.png') }}" id="img" class="img" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;">
@else
    <img src="" id="img" class="img" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;">
@endif
<input type="file" name="pathheader" id="pathheader"  class="form-control-file" aria-describedby="fileHelp" style="display:none;">

So when I render the view, it shows the images I thinks it's not practical to find it in server and click it again, if you don't want to change it.

所以当我渲染视图时,它会显示我认为在服务器上找到它并再次点击它是不现实的,如果你不想改变它的话。

Let me know your opinions, and if someone know how to do it i will be appreciated. (If know how to do it with some library or other functionalities let me know it.

让我知道你的意见,如果有人知道怎么做,我会很感激的。如果知道如何使用一些库或其他功能,请让我知道。

Thanks a lot.

非常感谢。

SOLVED

解决了

Easier way: Make data not required in controller.

更简单的方法:使数据在控制器中不需要。

Inside the function:

内部的功能:

public function updateProject(Request $request, $id) 
{
    $this->validate($request, array(
        'slug'=> 'required',
        'order'=> 'required',
        'public'=> 'required'
    ));
    $project = Project::find($id);
    $project->slug = $request->input('slug');
    $project->order = $request->input('order');
    $project->public = $request->input('public');
    if ($request->hasFile('pathheader')){
        $project->pathheader = $request->file('pathheader');
        \Storage::disk('projects')->putFileAs($project->slug,$project->pathheader,'header.png');
    }
    if ($request->hasFile('pathhome')){
        $project->pathhome = $request->file('pathhome');
        \Storage::disk('projects')->putFileAs($project->slug,$project->pathhome,'home.png');

    }
    $project->save();
    return redirect()->route('admin.projects.show', $project->id);
}

1 个解决方案

#1


2  

Due to browser's security, You cannot just put default value to file input.

由于浏览器的安全性,您不能只将默认值放入文件输入。

The better way is to check if the user selected a file before updating your records.

更好的方法是在更新记录之前检查用户是否选择了一个文件。

So on your update function:

更新功能:

public function update(){
    // Make sure you didn't required the user to select file.
    $attribute = [
        'name' => $request->name,
        'order' => $request->order
    ]
    if($request->hasFile('pathheader')){
        //If user select a file, upload the file 
        //Then you should update your record by 
        //adding fields to your update attribute. 
        $attribute['pathheader'] => $pathheader;

    }
    //otherwise, no changes will happen to your 'pathheader' column.
    DB::table('yourtable')->where('id',$id)->update($attribute);
}

#1


2  

Due to browser's security, You cannot just put default value to file input.

由于浏览器的安全性,您不能只将默认值放入文件输入。

The better way is to check if the user selected a file before updating your records.

更好的方法是在更新记录之前检查用户是否选择了一个文件。

So on your update function:

更新功能:

public function update(){
    // Make sure you didn't required the user to select file.
    $attribute = [
        'name' => $request->name,
        'order' => $request->order
    ]
    if($request->hasFile('pathheader')){
        //If user select a file, upload the file 
        //Then you should update your record by 
        //adding fields to your update attribute. 
        $attribute['pathheader'] => $pathheader;

    }
    //otherwise, no changes will happen to your 'pathheader' column.
    DB::table('yourtable')->where('id',$id)->update($attribute);
}