VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > .net教程 >
  • ASP.net教程之ifrem上传文件后显示

 
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">&times;</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("上传成功");
            }
        }
 
滴水能把石穿透,成事功到自然成
相关教程