CRUD with images, working with HttpPostedFileBase and byte []

4

We are using the EntityFramework with CodeFirst and DataBase PostgreSQL. There are some tables that should save images, and POCO classes are referenced as byte array.

Up to this point everything is quiet. I can get the images with HttpPostedFileBase parameters and convert them to byte[] to save to the database. The problem is when I want to retrieve this image from the database and put it in the <input type="file"/> field again.

The question is how to return this image in View Edit in the same <input type="file"/> field, as they do with the other fields of type string, int, etc.

    
asked by anonymous 25.08.2017 / 05:17

2 answers

2

For security reasons, you can not set a file as a value in a <input type="file"/> field. What you can do in this case is to put if in your view . If the file exists in model , you render a img tag or a download link. If it does not exist, you display <input type="file"/> to allow the image to be uploaded.

    
25.08.2017 / 13:36
2

As your friend commented, for security reasons you will not be able to do something like that. If you want to allow this person to download the already uploaded photo, you normally do an objectURL and the browser takes care of downloading it:

var file = new Blob([response.data], { type: 'image/bmp' });
var imageBlob = URL.createObjectURL(file);
window.open(imageBlob );

Reference: link

    
25.08.2017 / 13:45