コード例 #1
0
        /// <summary>
        /// 储存文件并返回结果
        /// </summary>
        /// <returns>Bool</returns>
        public bool Save()
        {
            if (!Directory.Exists(sv.MapPath(SavePath)))
            {
                Directory.CreateDirectory(sv.MapPath(SavePath));
            }
            if (ReqFile == null)
            {
                Err = 4;
                return(false);
            }
            if (ReqFile.ContentLength > MaxSize)
            {
                Err = 1;
                return(false);
            }
            var    fil          = Filter.Where(m => m == Path.GetExtension(ReqFile.FileName.ToLower()));
            string TempFilePath = TempFolder.EndsWith("/") ? TempFolder + NewFileName :  TempFolder + "/" + NewFileName;
            string NewFilepath  = SavePath.EndsWith("/") ? SavePath + NewFileName : SavePath + "/" + NewFileName;

            if (fil.Count() != 1)
            {
                Err = 2;
                return(false);
            }

            if (SafePicture)
            {
                try
                {
                    var rawimg = Image.FromStream(ReqFile.InputStream);
                    rawimg.Dispose();
                }
                catch (Exception)
                {
                    Err = 2;
                    return(false);
                }
            }

            try
            {
                ReqFile.SaveAs(sv.MapPath(TempFilePath));
            }
            catch (Exception)
            {
                Err = 3;
                return(false);
            }

            if (Resize)
            {
                IASPJpeg j = new ASPJpeg();
                j.Open(sv.MapPath(TempFilePath));
                j.Quality = 80;
                int ow = j.OriginalWidth;
                int oh = j.OriginalHeight;

                if (ow > MaxWidth)
                {
                    j.Width = MaxWidth;
                    if (Constrain)
                    {
                        j.Height = (int)((MaxWidth.CDbl() / ow.CDbl()) * oh.CDbl());
                        if (Crop && j.Height > MaxHeight)
                        {
                            j.Crop(0, (j.Height - MaxHeight) / 2, j.Width, MaxHeight + (j.Height - MaxHeight) / 2);
                        }
                    }
                    else
                    {
                        j.Height = MaxHeight;
                    }
                }

                j.Save(sv.MapPath(NewFilepath));
                StoredPath = NewFilepath;
                File.Delete(sv.MapPath(TempFilePath));
                return(true);
            }
            else
            {
                File.Move(sv.MapPath(TempFilePath), sv.MapPath(NewFilepath));
                StoredPath = NewFilepath;
                return(true);
            }
        }