Example #1
0
        /// <summary>
        /// 上传附件
        /// </summary>
        /// <param name="s">UploadFile对象</param>
        /// <param name="oldname">原文件名</param>
        /// <param name="UpFileDIR">UpFileDIR: 远程保存的地址,默认为"./"</param>
        /// <returns></returns>
        public string UploadAttFile(System.Web.HttpPostedFile s, string UpFileDIR, string oldname, string canextension, int length)
        {
            if (s.FileName.Trim() != "")
            {
                string res = "";
                try
                {
                    //对文件大小进行判断
                    if (length != 0)
                    {
                        if (s.ContentLength > length)
                        {
                            HttpContext.Current.Response.Write(PageFunc.ShowErrMsg("上传失败,文件大小超过" + length + "B"));
                            HttpContext.Current.Response.Write("<script language=javascript>history.go(-1);</script>");
                            HttpContext.Current.Response.End();
                        }
                    }


                    if (UpFileDIR.Trim() == "")
                    {
                        UpFileDIR = "/";                //保证规格为"/UploadFile/"
                    }

                    //上传文件
                    string extension = Path.GetExtension(s.FileName).ToUpper();

                    //对文件的扩张名进行判断
                    if (canextension.IndexOf(extension.Substring(1, extension.Length - 1)) == -1)
                    {
                        HttpContext.Current.Response.Write(PageFunc.ShowErrMsg("上传的文件类型不符合系统要求,请上传" + canextension + "类型的文件") + "<script language=javascript>history.go(-1);</script>");
                        //HttpContext.Current.Response.Write("<script language=javascript>history.go(-1);</script>");
                        HttpContext.Current.Response.End();
                    }

                    GC.Collect();
                    Random r        = new Random();
                    string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + r.Next(1000).ToString();
                    //return fileName;
                    string path = Server.MapPath(UpFileDIR + fileName + extension);
                    s.SaveAs(path);
                    GC.Collect();
                    res = UpFileDIR + fileName + extension;
                    //删除原来文件
                    string oldFilePath = Server.MapPath(UpFileDIR + oldname);
                    if (oldname != "" && File.Exists(oldFilePath))
                    {
                        File.Delete(oldFilePath);
                    }
                    res = res.Substring(res.LastIndexOf("/") + 1, res.Length - res.LastIndexOf("/") - 1);

                    /////////


                    return(res);
                }
                catch (Exception e)
                {
                    //Response.Write(Functions.ShowErrMsg("图片上传失败"));
                    //HttpContext.Current.Response.Write(Functions.ShowErrMsg(e.ToString()));
                    HttpContext.Current.Response.Write(PageFunc.ShowErrMsg("上传时出现错误,操作失败"));
                    HttpContext.Current.Response.Write("<script language=javascript>history.go(-1);</script>");
                    HttpContext.Current.Response.End();
                    // res = oldname;
                    // return res;
                }

                return(res);
            }
            return(oldname);
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="s">UploadFile对象</param>
        /// <param name="oldname">原文件名</param>
        /// <param name="UpFileDIR">UpFileDIR: 远程保存的地址,默认为"./"</param>
        /// <returns></returns>
        public string UploadFile(System.Web.HttpPostedFile s, string oldname, string UpFileDIR, string canextension, int length, int sm, int sm_Width, int sm_hight)
        {
            if (s.FileName.Trim() != "")
            {
                string res = "";
                try
                {
                    //对文件大小进行判断
                    if (length != 0)
                    {
                        if (s.ContentLength > length)
                        {
                            HttpContext.Current.Response.Write(PageFunc.ShowErrMsg("上传失败,文件大小超过" + length + "B"));
                            HttpContext.Current.Response.Write("<script language=javascript>history.go(-1);</script>");
                            HttpContext.Current.Response.End();
                        }
                    }


                    if (UpFileDIR.Trim() == "")
                    {
                        UpFileDIR = "/";                //保证规格为"/UploadFile/"
                    }
                    else
                    {
                        //上传目录不存在,则创建
                        if (!Directory.Exists(Server.MapPath(UpFileDIR)))
                        {
                            Directory.CreateDirectory(Server.MapPath(UpFileDIR));
                        }
                    }


                    //上传文件
                    string extension = Path.GetExtension(s.FileName).ToUpper();

                    //对文件的扩张名进行判断
                    if (canextension.IndexOf(extension.Substring(1, extension.Length - 1)) == -1)
                    {
                        HttpContext.Current.Response.Write(PageFunc.ShowErrMsg("上传的文件类型不符合系统要求,请上传" + canextension + "类型的文件") + "<script language=javascript>history.go(-1);</script>");
                        HttpContext.Current.Response.Write("<script language=javascript>history.go(-1);</script>");
                        HttpContext.Current.Response.End();
                    }

                    GC.Collect();
                    Random r        = new Random();
                    string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + r.Next(1000).ToString();
                    //return fileName;
                    string path      = Server.MapPath(UpFileDIR + fileName + extension);
                    string newSmPath = path.Replace(extension, "_sm" + extension);
                    s.SaveAs(path);
                    GC.Collect();
                    res = UpFileDIR + fileName + extension;
                    //删除原来文件
                    string oldFilePath = Server.MapPath(UpFileDIR + oldname);
                    if (oldname != "" && File.Exists(oldFilePath))
                    {
                        File.Delete(oldFilePath);
                    }
                    res = res.Substring(res.LastIndexOf("/") + 1, res.Length - res.LastIndexOf("/") - 1);

                    //当sm为1的时候,表明要生成缩略图
                    if (sm == 1)
                    {
                        ////////生成缩略图
                        //生成小图片
                        System.Drawing.Image image = new Bitmap(path);//得到原图


                        //创建指定大小的图
                        //System.Drawing.Image newImage = image.GetThumbnailImage(sm_Width, sm_hight, null, new IntPtr());


                        System.Drawing.Bitmap newImage = new Bitmap(sm_Width, sm_hight);
                        Graphics g = Graphics.FromImage(newImage);
                        //将原图画到指定的图上
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                        g.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;    //消除锯齿
                        g.DrawImage(image,
                                    new System.Drawing.Rectangle(0, 0, sm_Width, sm_hight),        //你的新大小
                                    new System.Drawing.Rectangle(0, 0, image.Width, image.Height), //你的原图大小
                                    System.Drawing.GraphicsUnit.Pixel);

                        g.Dispose();
                        newImage.Save(newSmPath);
                    }


                    /////////


                    return(res);
                }
                catch (Exception e)
                {
                    //Response.Write(Functions.ShowErrMsg("图片上传失败"));
                    //HttpContext.Current.Response.Write(Functions.ShowErrMsg(e.ToString()));
                    HttpContext.Current.Response.Write(PageFunc.ShowErrMsg("上传时出现错误,操作失败。" + e.ToString()));
                    HttpContext.Current.Response.Write("<script language=javascript>history.go(-1);</script>");
                    HttpContext.Current.Response.End();
                    // res = oldname;
                    // return res;
                }

                return(res);
            }
            return(oldname);
        }