上传照片(前台后台),显示

ProjectManager.aspx

前端
<%:Styles.Render("~/Content/bootstrap-table")%>
<%:Styles.Render("~/forms/artDialog/skins/default2.css")%>
<%:Styles.Render("../CSS/fileinput.min.css")%>
<%:Scripts.Render("~/bundles/bootstrap-table")%>
<%:Scripts.Render("~/bundles/bootstrap-dialog")%> <%:Scripts.Render("../JS/fileinput.min.js")%>  <%:Scripts.Render("~/forms/artDialog/artDialog.js")%>
 rows.push({
                field: 'Image',
                title: '项目图片',
                halign: 'center',
                align: 'center',
                width: '80px',
                formatter: function (value, row, index) {
                    if (!!value) {
                        return "<a  href='" + GetAbsoluteUrl() + value + "' target='_blank' ><img style='width:20px;height:20px;' src='" + GetAbsoluteUrl() + value + "'  /></a>";
                    } else {
                        return "  <a data-id='" + row.Id + "'  href=\"javascript:;\" οnclick=\"showUpalod('" + row.Id + "','Image')\">上传</a>";
                    }

                }
            });

<!-- 模态框1(Modal) -->
        <div class="modal fade" id="upImg" data-target="" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                            &times;
                   
                        </button>
                        <h4 class="modal-title" id="H1">上传文件
                        </h4>
                    </div>
                    <div class="box-body">
                        <div class="modal-body" id="liuchengbody">
                            <input type="hidden" id="key" />
                            <input id="faifile" name="faifile" type="file" data-show-preview="false">
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">
                            关闭
                        </button>
                    </div>
                </div>
                <!-- /.modal-content -->
            </div>
            <!-- /.modal -->
        </div>
        


  function showUpalod(id, type) {
            $('#key').val(id);
            initFileInput("faifile", "Project.ashx?action=FileUpload&type=" + type + "&rnd=" + Math.random());
            $('#upImg').modal('show');
        }

function initFileInput(ctrlName, uploadUrl) {
            var control = $('#' + ctrlName);
            control.fileinput('destroy');
            control.fileinput({
                uploadAsync: true,              //异步上传
                language: 'zh', //设置语言
                uploadUrl: uploadUrl, //上传的地址
                allowedFileExtensions: ['jpg', 'png', 'pdf'],//接收的文件后缀
                maxFileCount: 1, //表示允许同时上传的最大文件个数
                maxFileSize: 6144,//单位为kb,如果为0表示不限制文件大小
                showCaption: true,
                showUpload: true,
                showRemove: true,
                showClose: true,
                previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
                uploadExtraData: function (previewId, index) {   //额外参数的关键点
                    var data = {
                        id: $('#key').val()
                    };
                    return data;
                },
            }).on('fileuploaded', function (event, data, previewId, index) {
                var res = data.response; //返回结果
                if (res.result == "1") {
                    alert("上传成功!");
                    loadTb();
                } else {
                    alert("上传失败!");
                }

            });
        }
后台
 public void FileUpload(HttpContext context)
    {
        Dictionary<string, object> dic = new Dictionary<string, object>();
        try
        {
            HttpPostedFile file = context.Request.Files["faifile"];
            string id = context.Request["id"];
            string type = context.Request["type"];

            string savepath = "~/";  //父目录
            string fileName = GetOrderNumber();
            string day = DateTime.Now.ToString("yyyy-MM-dd");
            string Pfpath = context.Server.MapPath(savepath) + "UploadFile//StudyTour//" + day;

            if (!Directory.Exists(Pfpath))//查看存储路径的文件是否存在
            {
                Directory.CreateDirectory(Pfpath);   //创建文件夹,并上传文件
            }
            string imgUrl = fileName + "." + file.FileName.Substring(file.FileName.LastIndexOf('.') + 1);
            savepath = Pfpath + "//" + imgUrl;
            file.SaveAs(savepath);
            file.InputStream.Close();
            file.InputStream.Dispose();
            if (!string.IsNullOrEmpty(id))
            {
                AddProjectImg(Convert.ToInt64(id), "/UploadFile/StudyTour/" + day + "/" + imgUrl, type);

                Attachment attfile = new Attachment();
                attfile.ReferID = int.Parse(id);
                attfile.FormArea = type;
                attfile.FileName = file.FileName;
                attfile.ContentType = file.ContentType;
                attfile.FileSize = file.ContentLength;
                attfile.FileExt = file.FileName.Substring(file.FileName.LastIndexOf('.'));
                attfile.UploadUser = "admin";
                attfile.FilePath = "/UploadFile/StudyTour/" + day + "/" + imgUrl;
                attfile.UploadTime = DateTime.Now;
                attfile.IsEnable = true;
                attfile.ProcessType = (type == "pdf" ? "项目简介" : "项目封面");
                attfile.Insert();
            }

            dic.Add("result", "1");
        }
        catch (Exception ex)
        {
            //失败时返回的参数必须加 error键
            dic.Add("result", 0);
        }
        string json = Newtonsoft.Json.JsonConvert.SerializeObject(dic);
        context.Response.ContentType = "json/plain";
        context.Response.Clear();
        context.Response.Write(json);
        context.Response.End();
    }



/// <summary>
    /// 根据日期生成名称
    /// </summary>
    /// <returns></returns>
    public static string GetOrderNumber()
    {
        string num = DateTime.Now.ToString("yyMMddHHmmssfff");
        return num + Number(3, true).ToString();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值