CRUD with images , working with Httppostedfilebase and byte[]

Asked

Viewed 254 times

4

We are using Entityframework with Codefirst and Database Postgresql. There are some tables that should save images, and the POCO classes are referenced as byte array.

Up to that point all quiet. I can grab the images with Httppostedfilebase parameters and convert to byte[] to save to the database. The problem is when I want to rescue this image from the database and put it in the field <input type="file"/> again.

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

2 answers

1


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

  • I understood, and I thank you for the solution. But what if I want it to change the image? How would I?

  • 1

    In this case, you can provide a link to delete the image, and then display the <input type="file" /> again to allow the user to upload a new image.

1

As the friend commented, for security reasons you will not be able to do something like this. If you want to allow this person to download the already uploaded photo normally you make 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: https://developer.mozilla.org/en-US/docs/Web/API/Blob

Browser other questions tagged

You are not signed in. Login or sign up in order to post.