2014-04-10 4 views
0

데이터 테이블에 저장된 base64 문자열에서 이미지를 가져옵니다. 이 문자열은 성공적으로 가져오고 유효한 base64 문자열입니다. 무료 이미지 디코딩 웹 사이트에서 테스트 했으므로 원래 업로드 한 이미지로 다시 디코딩합니다.FileStream을 사용하여 이미지를 저장하지만 이미지가 유효하지 않습니다.

이제 파일에 이미지를 쓰려고하는데 시도해도 이미지 파일을 제대로 만들지 못합니다. 내가 File(imageBytes, "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와 함께 작동하는 이유는 무엇입니까?

답변

0

것은, 문제는이 시도 파일을

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

를 작성하십시오

[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(); 
     } 
관련 문제