即使出现错误,也会出现Sweetalert成功消息

时间:2017-04-18 01:28:26

标签: javascript laravel sweetalert

我正在尝试上传表单,如果一切正确,则会显示sweetalert成功消息(这样可以正常工作),但如果出现错误(例如图片太大),则应显示错误。问题是当出现错误时会显示成功消息,然后显示错误。

其他信息: *数据库正常工作,如果没有错误,它会插入新的寄存器,否则不会。 *我正在使用laravel 5.4。

我的控制器:

<?php

namespace App\Http\Controllers;

use Request;
use App\Programa;
use App;

class ProgramaController extends Controller{

  public function index(){
    return view('programa.create');
  }


  public function store(){

    $this->validate(request(), [

        'titulo' => 'required',
        'poster' => 'required|image|image_size:<=720',
    ]);

    $programa = new Programa;

    $programa->titulo = request('titulo');
    $name=request()->file('poster'). '.' . request()->file('poster')->getClientOriginalExtension();
    $programa->poster = request()->file('poster')->move('./uploads/', $name);

    $programa->save();

    return redirect('/');
  }

}

我的表格

<form method="POST" action="/crearPrograma" enctype="multipart/form-data" onsubmit='exito(this.id)'>

    {{ csrf_field() }}

    <div class="form-control">

        <label for="titulo">Título</label>

        <input type="varchar" class="form-control" id="titulo" name="titulo" placeholder="DoQu" required>

    </div>

    <hr>

    <div class="form-control-file">

        <input type="file" class="form-control-file" id="poster" name="poster" aria-describedby="fileHelp" required accept="image/*" >

        <small id="fileHelp" class="form-text text-muted">Póster</small>

    </div>

    <hr>



    <button type="submit" class="btn btn-primary">Crear</button>

    @include("layouts.errors")

</form>

我的js

<script>
  function exito(id){

    swal({
      title: "Guardado",
      text: "Se ha guardado correctamente",
      type: "success",
      timer: 2000,
      showConfirmButton: false
    });

  }
</script>

1 个答案:

答案 0 :(得分:1)

exito中检查文件大小。得到它:

//filesize would be in bytes
var filesize = document.getElementById("poster").files[0].size; 

然后:

function exito(id){
  var filesize = document.getElementById("poster").files[0].size;

  if(filesize <= 1000000){
    swal({
      title: "Guardado",
      text: "Se ha guardado correctamente",
      type: "success",
      timer: 2000,
      showConfirmButton: false
    });
  }else{
    //show your error here
  }
}