I have been tying to work it out since last couple of days... and finally hope i would get some answers from the lavarel community.
While working my Laravel project where I am trying to upload an image (single image) using either drag & drop or select.
NOTE: I have a placeholder image which i want to use it as a dropzone, and also the size may vary depending on where i want to use. Now I should be able to drop an image on this placeholder image. On drop or select the new image replaces the placeholder image (this part i am able to achieve) but when i am trying to submit the form if I have drop a file it won't get to the controller. But when i select the file i am good.
Here is what i have done so far... Can any one let me know what is wrong with the drag & drop method for submitting... the only issue i can think of is the <input type="file"> is not able to set the value when when file is dropped.
Here is my blade form
<form class="form" action="{{ route('categories.store') }}" method="post" enctype="multipart/form-data">
@csrf
<div class="form-group row">
<label for="category" class="col-sm-4 col-form-label">Category</label>
<div class="col-sm-8">
<input id="category" type="text" class="form-control @error('category') is-invalid @enderror" name="category" value="{{ old('category') }}">
@error('category')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="slug" class="col-sm-4 col-form-label">Slug</label>
<div class="col-sm-8">
<input id="slug" type="text" class="form-control @error('slug') is-invalid @enderror" name="slug" value="{{ old('slug') }}">
@error('slug')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="order" class="col-sm-4 col-form-label">Display Order</label>
<div class="col-sm-8">
<input id="order" type="number" class="form-control" name="order" value="{{ old('order') }}">
</div>
</div>
<div class="form-group row">
<label for="status" class="col-sm-4 col-form-label">Status</label>
<div class="col-sm-8">
<select id="status" class="form-control form__select" name="status">
<option value="1">Active</option>
<option value="0">Inactive</option>
</select>
</div>
</div>
<div class="form-group row">
<label for="file" class="col-sm-4 col-form-label">Upload Image</label>
<div class="col-sm-8">
<div class="upload-area" id="uploadfile">
{{-- <h1>Drag and Drop file here<br/>Or<br/>Click to select file</h1> --}}
<img class="img-fluid" src="{{ asset('img/640x480.png') }}" height="200px" id="file_preview" alt="img" />
</div>
<input type="file" name="file" id="file">
@error('file')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<div class="col-sm-12">
<button type="submit" class="form__submit">Save</button>
</div>
</div>
</form>
style.css
.upload-area{
width: 42%;
border: 2px solid lightgray;
border-radius: 3px;
text-align: left;
overflow: auto;
}
.upload-area:hover{
cursor: pointer;
}
.upload-area h1{
text-align: center;
font-weight: normal;
font-family: sans-serif;
line-height: 50px;
color: darkslategray;
}
#file{
opacity: 0;
}
and finally my JavaScript
// On File Drop
$('.upload-area').on('drop', function (e) {
e.stopPropagation();
e.preventDefault();
$(this).removeClass("dragover");
var file = e.originalEvent.dataTransfer.files[0];
readFile(file);
var fd = new FormData();
fd.append('file', file[0]);
// uploadData(fd);
});
// Open file selector on div click
$("#uploadfile").click(function () {
$("#file").click();
});
// file selected
$("#file").change(function () {
var file = $('#file')[0].files[0];
readFile(file);
});
function readFile(file) {
var reader = new FileReader();
reader.onload = function (e) {
$('#file_preview').attr('src', e.target.result);
};
reader.readAsDataURL(file);
}