ifrem上傳文件後顯示 1、上傳文件按鈕 <a class="btn btn-primary pull-right" id="data-upload" style="margin-right:10px;" data-target="#UploadFiles" data-toggle="modal" ...
ifrem上傳文件後顯示 1、上傳文件按鈕 <a class="btn btn-primary pull-right" id="data-upload" style="margin-right:10px;" data-target="#UploadFiles" data-toggle="modal">上傳報告</a> 2、上傳文件彈出的模態對話框 <!-- 上傳報告 --> <div class="modal fade" id="UploadFiles" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" id="closeReportId" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> <h4 class="modal-title" id="myModalLabel">上傳報告</h4> </div> <div class="modal-body" style="min-height:450px;"> <iframe name="upframe" id="upframe" style="width:100%; height:300px; border:0;"></iframe> </div> <div class="modal-footer"> <button id="btn-upload" class="btn btn-primary">上傳</button> <button type="button" class="btn btn-default" data-dismiss="modal">關閉</button> </div> </div> </div> </div> 3、點擊彈出按鈕觸發的事件 $('#data-upload').click(function(){ $('#upframe').attr("src","@Url.Action("Upload", "Report",new { id=Model.Project.Id})"); }); 4、上傳文件頁面顯示的內容 @model Project @{ Layout = null; } @using (Html.BeginForm("UploadFile", "Report", FormMethod.Post, new { enctype = "multipart/form-data" })) { <p> 已傳報告: @(string.IsNullOrEmpty(Model.ReportFilePath)?"無": Model.Title.ToString()+".doc") </p> <input id="reportFile" name="file" type="file" /> <input type="hidden" value="@Model.Id" name="id" /> } 5、點擊上傳按鈕處理的事件 $('#btn-upload').click(function(){ $(window.frames["upframe"].document).find("form").submit(); }); 6、上傳文件後,控制器中的處理 /// <summary> /// 上傳項目報告 /// </summary> /// <param name="id">項目ID</param> /// <param name="file"></param> /// <returns></returns> [HttpPost] public ActionResult UploadFile(int id, HttpPostedFileBase file) { if (file == null) return Content("沒有選擇文件"); if (AuthContext.Current == null) return Content("登錄異常!"); using (var db = new Entities()) { var model = db.Project.Find(id); if (model == null) return Content("項目異常!"); string path = String.Format(@"Control\{0}\{1}\{2}.hfrpt", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.ToString("dd-hhmmss")); var key = FileHelper.GetFileName(path); var temp = FileHelper.GetFileName(String.Format(@"Temp\{0}.hfrpt", DateTime.Now.ToString("yyyyMMdd-hhmmss"))); file.SaveAs(temp); var safekey = id.ToString() + "haifeng%"; EncryptHelper.DesEncrypt(temp, key, safekey); System.IO.File.Delete(temp); model.ReportFilePath = path; var entry = db.Entry(model); entry.State = EntityState.Modified; db.SaveChanges(); return Content("上傳成功"); } }