欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ifrem上传文件后显示

程序员文章站 2022-06-28 20:47:54
ifrem上传文件后显示 1、上传文件按钮
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("上传成功");
            }
        }