你的浏览器禁用了JavaScript, 请开启后刷新浏览器获得更好的体验!
输入关键字进行搜索
搜索:
没有找到相关结果
豁达于心
赞同来自:
<?php namespace Admin; use View; use Response; use Input; use Validator; class AdminUploadController extends Controller { public function postFile() { $file = Input::file('file'); $ext = $file->guessClientExtension(); $input = array('file' => $ext); $rules = array( //'image' => 'image' 'file' => 'in:'.Input::get('ext') ); $messages = array( 'file.in' => '上传的文件格式不正确,只能是'.Input::get('ext').'格式!' ); $validator = Validator::make($input, $rules, $messages); if ( $validator->fails() ) { return Response::json(['success' => false, 'msg' => $validator->getMessageBag()->toArray()]); } else { $path = '/upload/file/'.date('Ymd').'/'; $destinationPath = public_path() . $path; $filename = $file->getClientOriginalName(); $newfile = md5(date('YmdHis').$filename).'.'.$ext; $file->move($destinationPath, $newfile); return Response::json(['success' => true, 'msg' => '上传成功!', 'file' => $path.$newfile]); } } }
{{ Form::open(array('url' => action('Admin\AdminUploadController@postFile'), 'class' => 'form-horizontal ajax-form-upload', 'role' => 'form', 'files' => 'true')) }} <div class="modal-header"> <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button> <h4 class="modal-title" id="myModalLabel">文件上传</h4> </div> <div class="modal-body"> {{ Form::file('file', array('class' => 'form-control', 'required')) }} {{ Form::hidden('ext','jpeg,jpg,gif,bmp,png,swf') }} </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">取消</button> <input type="submit" class="btn btn-primary" value="上传" data-input="fileInput"> </div> {{ Form::close() }}
/* * Ajax POST 上传单文件 * 用法:<form class="ajax-form-upload">...</form> * 注:需要结合jquery.form.js $.scojs_message()为sco.message.js信息提示插件 */ $('form.ajax-form-upload').submit(function(e){ e.preventDefault(); var _url = $(this).attr('action'), _data = $(this).serializeArray(), _btn = $(this).find('[type=submit]'), _inputId = '#'+ _btn.attr('data-input'), _btn_v = _btn.val(); //alert(_inputId) $(this).ajaxSubmit({ type: "POST", url: _url, data: _data, //dataType: 'json', beforeSend: function(){ _btn.attr("disabled", true); _btn.val( _btn_v + '中...'); }, success: function(data){ if (data.success) { $.scojs_message(data.msg, $.scojs_message.TYPE_OK); setTimeout(function(){ $(_inputId).val(data.file); $('#myModal').modal('hide'); $('#myModal').find('input[type=file]').val(''); _btn.val( _btn_v ); _btn.attr("disabled", false); },2000); } else { var _msg = data.msg, _str = ''; if($.type(data.msg) == 'string') { _str = _msg; } else { $.each(_msg,function(index, value){ _str += value + ' '; }); } $.scojs_message(_str, $.scojs_message.TYPE_ERROR); _btn.val( _btn_v ); _btn.attr("disabled", false); } }, error: function(){ alert('Oh My God! 出错了,请稍候再试....'); _btn.val( _btn_v ); _btn.attr("disabled", false); } }); return false; });
test
要回复问题请先登录或注册
2 个回复
豁达于心
赞同来自:
上传控制器
HTML
把代码中的{{ Form::hidden('ext','jpeg,jpg,gif,bmp,png,swf') }}改成你需要上传的文件的后缀即可
jQuery
test
赞同来自:
$filename = $file->getClientOriginalName();
这里面的方法写的是什么呢?
jquery.form.js
sco.message.js
还有这些JS 网上找不到呢 可以发给我吗?