Instead of re-inventing the wheel, take a look at www.deopzonejs.com, Jeffrey gave a tutorial using Dropzonejs in this series https://laracasts.com/series/build-project-flyer-with-me, It can be customized to upload a single file as well as providing basic mime type checks for you on the client side.
Jan 30, 2016
3
Level 17
Ajax Submit For Image Upload
I been doing all this all day , finding a way , searching thru google and laracast but it seems i cant find the correct guide or tutorial ,
at the moment here is my code this is my js script
function submitReceipt(id){
var url = $('#postReceiptForm'+ id).attr('action');
var formdata = $('#postReceiptForm' + id).serializeArray();
$.ajax({
url: url,
dataType:'JSON',
data: formdata,
type:'post',
}).done(function(data){
Materialize.toast(data.message, 4000,'',function () {
console.log(data);
});
}).fail(function () { // if Fail
var errors = data.responseJSON;
$.each(errors.message, function(index, error)
{
Materialize.toast(error, 4000,'',function(){
//
});
});
});
}
here is my blade template
<form action="postReceipt" method="POST" id="postReceiptForm{{ $order->id }}" enctype="multipart/form-data" onsubmit="submitReceipt({{ $order->id }}); return false;">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
<input type="hidden" name="id" value="{{ $order->id }}"/>
<div class="file-field input-field small">
<div class="btn">
<span>Upload Your Receipt</span>
<input type="file" name="attachment">
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
</div>
<div class="modal-footer">
<button class="col s6 pull-m1 m5 pull-l1 l5 teal lighten-3 btn-large modal-action modal-close waves-effect waves-light btn-flat" type="submit" name="action" >Upload Receipt</button>
<a href="#!" class="col s6 push-m1 m5 push-l1 l5 left red lighten-2 btn-large modal-action modal-close waves-effect waves-light btn-flat">Close</a>
</div>
</form>
Normal issue im facing csrf token mismatch and File is NULL ...
Anyone can help me to get me thru with this would be Awesome thanks
Please or to participate in this conversation.