C# MVC 文件字节流 保存

   public string  Upload(string Bytes)
        {
         try
            {

                byte[] bytes = Convert.FromBase64String(Bytes);

                string times = DateTime.Now.ToString("yyyy-MM-dd");
                //建立存储的目录
                string ImageFileUrl = "Myfiles/" + times + "/";
                //判断目录是否存在
                if (!Directory.Exists(Server.MapPath(ImageFileUrl)))
                {
                    //如果不存在,创建
                    Directory.CreateDirectory(Server.MapPath(ImageFileUrl));
                }
                string NewFilethumbName = Guid.NewGuid().ToString() + ".pdf";  //图片命名
                string ImageFile = "/allapi/" + ImageFileUrl + NewFilethumbName;  //返回路径
                if (bytes != null)
                {
                    using (System.IO.MemoryStream mem = new MemoryStream(bytes))
                    {

                        string NewFileJpg = Server.MapPath("~/allapi/" + ImageFileUrl) + NewFilethumbName; //保存路径

                        FileStream fs = new FileStream(NewFileJpg, FileMode.OpenOrCreate);
                        fs.Write(bytes, 0, bytes.Length);
                        fs.Close();
                    }
                }

                return ImageFile;
            }
            catch (ArgumentException e)
            {
                return e.Message;

            }

        }

猜你喜欢

转载自blog.csdn.net/qq_39103373/article/details/91979114