Getting mimes validation fail when not uploading optional file with AJAX

Posted 4 months ago by Gabotronix

Hi, I'm trying to make a bug report system where the user sends a body of text and OPTIONALLY he can upload a file (image, doc...) along with the text to be sent to my mail inbox.

However, when I only send text I get this error:

"The file must be a file of type: doc, pdf, docx, zip, odt, jpeg, bmp, png, jpg, svg."

how can I fix this, also when I upload a file along with text the mail is sent all right but the attached file is only 1kb, always.

Jquery code:

var formData = new FormData();
        formData.append('body', $('#bug_body').val());
        formData.append('file', $('#bug_file').prop('files')[0]);
        
        
        $.ajaxSetup({
            
            headers: {'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')}
        
        });
        
        $.ajax({
            
            async: true,
            url: '/contactar-mail-bug',
            type: 'POST',
            data: formData,
            contentType: false,
            processData: false, 
            dataType: 'JSON',
            
            success: function (data) { 
                $('.form_valid_container').fadeIn().html('<span class="form_valid_text">✓ '+ data.success +'</span>');
                form.trigger("reset");
                console.log(data.success);
            },
            
            error: function (data){
                var errors = data.responseJSON;
                console.log(errors);
                
                $.each(errors , function(){
                    $('.form_error_container').fadeIn().html('<span class="form_error_text">✘ '+ errors.message +'</span>')
                }); 
            }

        });

Method for sending email:

public function contactar_reportar_bug(BugReport $request)
    {
        $sender_mail = config('globals.admin_mail');//mail sender, in this case it's me
        
        $destination_mail = config('globals.admin_mail');//mail receiver, in this case it's me too
        
        $client_phone = config('globals.client_phone');
        
        $body = $request->body;//bug report mail message
        
        $file = $request->file('file');//bug report screencap or doc
        
        $file_name = $file->getClientOriginalName();//outputs Laravel DOC.odt
        
        $mime_type = $file->getMimeType();//outputs application/
        
        $file_extension = $file->guessExtension();//outputs .odt
        
        $restaurant = Globals::where('name' , 'Nombre del restaurante')->pluck('value');//outputs web_name, used in the mail subject
        
        $subject = "Nuevo reporte de bug en $restaurant";//subject of the mail
        
        
        
        
        $bug = new Bug();
        $bug->body = $request->body;
        $bug->save();
        
        
        
        Mail::to($destination_mail)->send(new BugReportMail($body, $file, $subject, $restaurant, $client_phone, $sender_mail, $file_name, $mime_type));
        
        return response()->json([
        
            'success' => 'Reporte de bug enviado',
            'bug' => $bug,

        ]);
    }

Bug Report form request (notice how only the body of the message is required):

public function rules()
    {
        return [
            
            'body' => 'required|string|min:5',
            
            'file' => 'mimes:doc,pdf,docx,zip,odt,jpeg,bmp,png,jpg,svg|max:2000'
        ];
    }

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

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.