Ejemplo n.º 1
0
        public static string UploadImage(System.Web.UI.HtmlControls.HtmlInputFile file, string basePath)
        {
            jsHelper js = new jsHelper();

            if (file.PostedFile.ContentLength > 1024 * 1024)
            {
                jsHelper.Alert("上传文件不得大于1M");
                return("");
            }
            if (!file.PostedFile.FileName.ToLower().EndsWith(".jpg"))
            {
                jsHelper.Alert("只能上传jpg图片");
                return("");
            }
            string tempPath = string.Format("{0}/{1}/{2}", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);

            tempPath = string.Format("{0}/{1}", basePath.TrimEnd(new char[] { '/' }), tempPath);
            string tempFile = string.Format("{0}.jpg", BitConverter.ToUInt32(Guid.NewGuid().ToByteArray(), 0).ToString());
            string fullPath = HttpContext.Current.Server.MapPath(tempPath);

            CDirectory.Create(fullPath);
            file.PostedFile.SaveAs(Path.Combine(fullPath, tempFile));
            string sReturnUrl = string.Format("{0}/{1}", tempPath, tempFile);

            return(sReturnUrl);
        }
Ejemplo n.º 2
0
        public Dictionary <string, string> GeneratePath()
        {
            if (string.IsNullOrEmpty(WebPath.ImagePath))
            {
                throw new Exception("配置文件中找不到ImagePath");
            }
            lock (_dicImgTag)
            {
                _dicImgTag.Clear();
                string s        = string.Empty;
                string copyto   = string.Empty;
                string Imagedir = string.Empty;
                MD5    m        = MD5CryptoServiceProvider.Create();
                foreach (string encodechar in EncodeHTML.EncodeChars)
                {
                    s = DateTime.Now.Ticks.ToString();
                    s = s.Insert(DateTime.Now.Millisecond % s.Length, encodechar);
                    s = GetString(m.ComputeHash(Encoding.UTF8.GetBytes(s)));
                    //Imagedir = ImgHelp.GetImageDirectory();
                    copyto = string.Format("{0}/enc{1}{2}.gif", WebPath.ImagePath, Imagedir, s);
                    CDirectory.Create(copyto.Substring(0, copyto.LastIndexOfAny(new char[] { '\\', '/' })));
                    File.Copy(string.Format("{0}{1}", WebPath.ImagePath, dicOriginalPath[encodechar]), copyto);

                    _dicImgTag.Add(encodechar, string.Format("<img src=\"http://img.39.net/enc/{0}{1}.gif\"/>", Imagedir, s));
                }
                m.Clear();
            }

            return(_dicImgTag);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="inputStream">输入数据流</param>
        /// <param name="sRootDirectory">保存的根目录(不要包括根据时间自动生成的目录)</param>
        /// <param name="sExtension">扩展名</param>
        /// <returns>返回根据时间自动生成的文件路径</returns>
        public static string SaveImage(Stream inputStream, string sRootDirectory, string sExtension)
        {
            sRootDirectory = sRootDirectory.TrimEnd('/', '\\');

            string sReturn = GetImageDirectory() + CreateFileName(sExtension);
            string path    = sRootDirectory + sReturn;

            CDirectory.Create(Path.GetDirectoryName(path));
            using (FileStream fStream = File.Create(path))
            {
                int    size = 2048;
                byte[] data = new byte[2048];
                while (true)
                {
                    size = inputStream.Read(data, 0, data.Length);
                    if (size > 0)
                    {
                        fStream.Write(data, 0, size);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            return(sReturn);
        }
Ejemplo n.º 4
0
        public static string  UploadFile(System.Web.UI.HtmlControls.HtmlInputFile file, string basePath)
        {
            if (file.PostedFile.ContentLength > 1024 * 1024)
            {
                jsHelper.Alert("上传文件不得大于1M");
                return("");
            }
            if (!file.PostedFile.FileName.ToLower().EndsWith(".jpg"))
            {
                jsHelper.Alert("只能上传jpg图片");
                return("");
            }
            string tempPath = string.Format("{0}/{1}/{2}", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            string tempFile = string.Format("{0}.jpg", BitConverter.ToUInt32(Guid.NewGuid().ToByteArray(), 0).ToString());

            CDirectory.Create(Path.Combine(basePath, tempPath));
            file.PostedFile.SaveAs(Path.Combine(Path.Combine(basePath, tempPath), tempFile));
            string sReturnUrl = string.Format("http://bbsimg.39.net/{0}/{1}", tempPath, tempFile);

            return(sReturnUrl);
        }
Ejemplo n.º 5
0
 public static void SaveImage(Stream inputStream, string sFilePath)
 {
     CDirectory.Create(Path.GetDirectoryName(sFilePath));
     using (FileStream fStream = File.Create(sFilePath))
     {
         int    size = 2048;
         byte[] data = new byte[2048];
         while (true)
         {
             size = inputStream.Read(data, 0, data.Length);
             if (size > 0)
             {
                 fStream.Write(data, 0, size);
             }
             else
             {
                 break;
             }
         }
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        /// 保存缩略图片
        /// </summary>
        /// <param name="inputStream">输入数据流</param>
        /// <param name="sRootDirectory">保存的根目录(不要包括根据时间自动生成的目录)</param>
        /// <param name="sExtension">扩展名</param>
        /// <param name="iWidth">图片宽度(缩略后)</param>
        /// <param name="iHeight">图片高度(缩略后)</param>
        /// <returns></returns>
        public static string SaveThumbnailImage(Stream inputStream, string sRootDirectory, string sExtension, int iWidth, int iHeight)
        {
            Image image = Image.FromStream(inputStream);

            image = image.GetThumbnailImage(iWidth, iHeight, null, System.IntPtr.Zero);

            MemoryStream newStream = new MemoryStream();

            image.Save(newStream, GetImageType(sExtension));
            newStream.Seek(0, SeekOrigin.Begin);

            //保存
            sRootDirectory = sRootDirectory.TrimEnd('/', '\\');

            string sReturn = GetImageDirectory() + "T" + CreateFileName(sExtension);
            string path    = sRootDirectory + sReturn;

            CDirectory.Create(Path.GetDirectoryName(path));
            using (FileStream fStream = File.Create(path))
            {
                int    size = 2048;
                byte[] data = new byte[2048];
                while (true)
                {
                    size = newStream.Read(data, 0, data.Length);
                    if (size > 0)
                    {
                        fStream.Write(data, 0, size);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            return(sReturn);
        }
Ejemplo n.º 7
0
 public static void Write(FileEntity file)
 {
     CDirectory.Create(file.FilePath);
     Write(file.FilePath + "\\" + file.FileName, file.FileContent);
 }