Ejemplo n.º 1
0
        public static string SaveFileForBase64(string file, string fileName)
        {
            string result = "";

            try
            {
                string directory = HttpRuntime.AppDomainAppPath.ToString();
                string path      = "/Upload";
                byte[] content   = Convert.FromBase64String(file.Substring(file.IndexOf("base64,") + 7));//切除前面那段image标识
                string extension = Path.GetExtension(fileName);

                CreateCatalog(directory, path);

                path = string.Format("{0}/{1}", path, DateTime.Now.ToString("yyyyMMdd"));
                CreateCatalog(directory, path);

                path = string.Format("{0}/{1}{2}{3}", path, DateTime.Now.ToString("yyyyMMddHHmmssfff"), new Random(GetRandomSeed()).Next(1, 1000), extension);

                using (MemoryStream ms = new MemoryStream(content))
                {
                    Bitmap bm = new Bitmap(ms);
                    bm.Save(directory + path.Replace("/", "\\"));
                }

                if (File.Exists(directory + path.Replace("/", "\\")))
                {
                    result = path;
                }
            }
            catch (Exception ex)
            {
                Log4Helper.WriteLog("保存文件失败", ex);
            }

            return(result);
        }