louis12
5 days ago
118
8
Laravel

Dropzone upload paths and name attributes.

Posted 5 days ago by louis12

Good day,

I am trying to replace individual file / image upload fields with Dropzone. After adding the Dropzone code, the images are now uploading to an individual folder: (storage_path('app/public/images')

Could someone please advise on how I could assign different upload paths for each listing (post), as well as how I could assign a different name attribute to each image ( in order to store the image names in different columns in the database)?

Please find the code below.

View file:

 <form method="post" action="{{ route('dropzone.store') }}" enctype="multipart/form-data"
          class="dropzone" id="dropzone">
        @csrf
        </form>
    </div>
    <script type="text/javascript">
        Dropzone.options.dropzone =
        {
            maxFilesize: 10,
            renameFile: function (file) {
                var dt = new Date();
                var time = dt.getTime();
                return time + file.name;
            },
            acceptedFiles: ".jpeg,.jpg,.png,.gif",
            addRemoveLinks: true,
            timeout: 60000,
            success: function (file, response) {
                console.log(response);
            },
            error: function (file, response) {
                return false;
            }
        };
    </script>

Controller:


class ImageController extends Controller
{
    public function index()
    {
    	return view('image');
    }

    public function store(Request $request)
    {
    	$image = $request->file('file');
        $avatarName = $image->getClientOriginalName();
        $image->move(storage_path('app/public/images'),$avatarName);
         
        $imageUpload = new Image();
        $imageUpload->filename = $avatarName;
        $imageUpload->save();
        return response()->json(['success'=>$avatarName]);
    }
}

Please sign in or create an account to participate in this conversation.