Update an Image uploaded to SQL Server in ASP.NET

2019-09-13 11:10发布

问题:

I have successfully uploaded an Image on my SQL Server using ASP.NET MVC 3 C#.

Also I have managed to successfully displayed/downloaded that image in the Index view.

However, the problem occurs when in the Edit view of the data,

If the Image has been changed by selecting the file in the view, the new image stores in the DB and everything is okay.

But when the user does not change the image, the value stored in the SQL Server is a null?

How to avoid this.

i.e, if the image is not selected/updated in the edit view, the previously stored image in the database should not be updated to a null value.

回答1:

The problem is that when the user doesn't change the image the post doesn't contain the image data. You are bluntly updating all the fields in your table and therefore set image field to NULL.

You can either check if you've actually received an image in the post and do not set it to NULL in update statement, or extract the image upload to separate action. It may be even more friendly for the user to have a clearly separated UI for image upload (otherwise they could be confused: if I edit some other fields, will my image disappear? )



回答2:

For the edit page, you should have add default post data which was previously stored.

If you are not giving default value in the edit form (default value should come from previously stored data), the form will be submitted with NULL data.

In that case you will get NULL during update.


Hope this question will be a great help for you-

How update/edit uploaded files in asp.net mvc 5 without redundancy?