前台引用js:
<script src="~/Resource/Script/FileUpload/js/jquery.min.js"></script>
<link href="~/Resource/Script/FileUpload/css/fileinput.min.css" rel="stylesheet" /> <script src="~/Resource/Script/FileUpload/js/fileinput.min.js"></script> <script src="~/Resource/Script/FileUpload/js/locales/zh.js"></script>
前台:
<input type="file" multiple id="smallTuBiao" /> <script type="text/javascript"> $(function () { $("#smallTuBiao").fileinput({ language: 'zh', //设置语言 uploadUrl: "/FileUpload/FileUpload", //上传的地址 allowedFileExtensions: ['jpg', 'png', 'gif', 'xls', 'xlsx', 'doc', 'docx', 'txt', 'ppt', 'pptx', 'pdf'],//接收的文件后缀 showUpload: true, //是否显示上传按钮 showCaption: false,//是否显示标题 fileActionSettings: { showUpload: false, showZoom: false }, showAjaxErrorDetails: false, maxFileCount: 10, maxFileSize: 50000, showUploadedThumbs: false, overwriteInitial: false, showClose: false, enctype: 'multipart/form-data', browseClass: "btn btn-primary", //按钮样式 previewFileIcon: "<i class='glyphicon glyphicon-king'></i>" //slugCallback: function (filename) { // //这个方法得到文件名字 // return filename.replace('(', '_').replace(']', '_'); //} }); //.on('fileuploaded', function (event, file, previewId, index) { // alert('i = ' + index + ', id = ' + previewId + ', file = ' + file.name); //}); }); </script>
后台:
public JsonResult FileUpload()
{ ResultJson rj = new ResultJson(); //try //{ using (TransactionScope ts = new TransactionScope()) { //打开共享目录 WebCommon.SetNetPostion(); var file = Request.Files[0]; string priblemID = Session["FDBProblemID"].ToString(); TProblemBusiness probleeBusiness = new TProblemBusiness(); var pr = probleeBusiness.GetTProblem(priblemID); DBStateEnum state = DBStateEnum.Normal; if (pr == null) { state = DBStateEnum.Draf; } //新名字 string newFileName = WebCommon.UploadFilePath + Guid.NewGuid() + Path.GetExtension(file.FileName); //保存文件 file.SaveAs(newFileName); //关闭 WebCommon.CancelNetPostion(); //记录到数据库 TAttachment ta = new TAttachment(); ta.CreateTime = DateTime.Now; ta.CreateUserID = new Guid(WebCommon.GetLoginUser().UserID); ta.DBState = Convert.ToInt32(state).ToString(); ta.FileName = file.FileName; ta.FilePath = newFileName; ta.ID = Guid.NewGuid(); ta.TSoruceID = new Guid(priblemID); TAttachmentBusiness fileBusiness = new TAttachmentBusiness(); rj = fileBusiness.Add(ta); ts.Complete(); } //} //catch (Exception ex) //{ // //关闭共享目录 // WebCommon.CancelNetPostion(); // rj.Msg = ex.Message; // rj.Result = false; //} return Json(rj, JsonRequestBehavior.AllowGet); }