0

Trying to download Image which is stored sql server in the form of bytes and was able to download the image, but i'm getting the corrupted image and was not able to open the downloaded image

Here is my code

data.imageData is the Byte[] data of image which is retrieved from SQl server

data.imageName is the Name of file

public class Attachmets
 {
    public byte[] Imagedata {get;set;}
    public string ImageName {get;set;}
  }

public static HttpResponseMessage DownloadImage(Attachments data, bool isInline)
        {
            isInline = false;
            var response = new HttpResponseMessage();

            var memoryStream = new MemoryStream(data.ImageData);
            response.Content = new StreamContent(memoryStream);

            var headers = response.Content.Headers;
            headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            headers.ContentDisposition =
                new ContentDispositionHeaderValue(isInline ? DispositionTypeNames.Inline : DispositionTypeNames.Attachment)
                {
                    FileName = Path.GetFileName(data.ImageName)
                };
            return response;
        }
chinna g
  • 405
  • 1
  • 4
  • 6
  • The type of the `data` parameter is `Attachments`... what's that? Are you sure that the `Data` member is an encoded octet stream, because that's what you're telling the browser it is. If it isn't, then garbage-in, garbage-out. – Gary McGill Feb 08 '18 at 16:24
  • @GaryMcGill update my question DownloadImage is the function name to which i'm passing the data. Attachmets is the class file which is having imagedata and imageName – chinna g Feb 08 '18 at 17:16

2 Answers2

1

If you're using Web API 2 (as tagged) and retrieving data from SQL Server (although confusingly this is not shown in the question code, where you appear to be just returning data which is submitted from the client, but we'll leave that to one side), you can do something like this using a FileResult, and a byte array (which is what an image or binary field in SQL Server will become in .NET):

If you have a Document class like this:

public class Document
{
  public byte[] Data { get; set; }
  public string FileName { get; set; }
  public string MimeType { get; set; }
}

you can have a controller action method like this:

public IHttpActionResult Download(int id) { //id will be the documentID to retrieve from the database

  //Then, here you place your database code to retrieve the correct data from the database including the binary file data, a file name and a suitable Mime Type. 
  //Then populate that into an instance of the Document class (calling the variable "doc" and then simply return it to the client like this:

  return new FileResult(doc.Data, doc.FileName, doc.MimeType);
}
ADyson
  • 57,178
  • 14
  • 51
  • 63
0

am not sure how you are handling in client side. but you are directly binding the response. This should work

    public FileResult DownloadImage(Attachments data, bool isInline)

    {
       ...
           ...


        return new FileStreamResult(new FileStream(path, FileMode.Open), "image/jpeg");
    }

Where you are returning the file Stream to the browser and browser will render it as image

Would recommend referring Can an ASP.NET MVC controller return an Image? where you have lot of options to try as per your need

Peru
  • 2,871
  • 5
  • 37
  • 66