Asp.net mvc 使用FileStream保存图像,但图像无效

Asp.net mvc 使用FileStream保存图像,但图像无效,asp.net-mvc,image,asp.net-mvc-4,base64,filestream,Asp.net Mvc,Image,Asp.net Mvc 4,Base64,Filestream,我从一个base64字符串获取一个图像,存储在一个datatable中。这个字符串成功提取,是一个有效的base64字符串,因为我在免费图像解码网站上测试过它,它将它解码回我最初上传的图像 现在,我正在尝试将图像写入文件,但无论我如何尝试,它都无法正确创建图像文件。仅当我将其作为FileimageBytes、image/jpeg返回视图时,图像才会显示 这是我的密码: string imagepath = Path.Combine(Server.MapPath("~/Co

我从一个base64字符串获取一个图像,存储在一个datatable中。这个字符串成功提取,是一个有效的base64字符串,因为我在免费图像解码网站上测试过它,它将它解码回我最初上传的图像

现在,我正在尝试将图像写入文件,但无论我如何尝试,它都无法正确创建图像文件。仅当我将其作为FileimageBytes、image/jpeg返回视图时,图像才会显示

这是我的密码:

            string imagepath = Path.Combine(Server.MapPath("~/Content"), client.ClientId + "_task" + task.TaskId + "_TaskReport.jpg");

            byte[] imageBytes = Convert.FromBase64String(myDataTable.Rows[0].ItemArray[1].ToString()); // this works elsewhere, but for some reason only when returning the image to the view as a FileResult


            using (var imageFile = new FileStream(imagepath, FileMode.Create))
            {
                imageFile.Write(imageBytes, 0, imageBytes.Length); // this line creates the bad image!
                imageFile.Flush();
            }
我的代码有什么问题?为什么它在转换为图像时与mvc FileResult一起工作而不工作?

尝试编写此文件

byte[] b= //Your Image File in bytes;
System.IO.File.WriteAllBytes(@"c:\data.jpg", b)
试试这个

[HttpPost]
        public ActionResult FileUpload(HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (file != null && file.ContentLength > 0)
                    {                            
                        System.IO.MemoryStream target = new MemoryStream();
                        file.InputStream.CopyTo(target);
                        byte[] data = target.ToArray();                        
                    }
                }
                catch (Exception ex)
                {
                    throw;
                }
                return View("Index",model);
            }
            return View();
        }