Add a file upload to an ASP.NET project
Hi, I've kind of messed up and mistook a deadline on a project for anther, and suddenly found myself very behind on work, and I will need some help.
We have a web store project which lets you add products as a seller and then buy them as a buyer. This works fine, but we want to add pictures of the products aswell.
This is in our controller:
This is our model:
Now, I'm somewhat sure
PictureId
will not be good enough in this case? I would either need picture data itself in order to throw a data:image
sort of thing into the page, or a path to the picture.
I am attempting to follow https://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-6.0#upload-small-files-with-buffered-model-binding-to-a-database, but as with most Microsoft guides, I sadly find it too technical to really follow. I get lost easily with what part of the code is what '^^Upload files in ASP.NET Core
How to use model binding and streaming to upload files in ASP.NET Core MVC.
1 Reply
okay scratch the above, I didn't go with saving to database, I went with saving locally instead. I know local saving from another project I did with PHP, so I'm much more comfy with this concept.
I did still run into an issue however, while the new view does show the proper picture field, uploading it doesn't work. I'm receiving "The picture field is required" on clicking "Create", with no apparent way to continue. Any ideas?
Current model:
Current controller:
View:
It worked! I noticed that the
ModelView.IsValid
returned false
, and the errors were pointing to PictureLink.
So I just made that Nullable, and now the picture has successfully been uploaded :D
Sadly now I'm struggling to show the picture, in a way.
I have 2 records, one where PictureLink is NULL, one where it's a filename.
Seems that no matter what condition I put into the if statement, it always seems to return the same value for both...
swear to god I need a rubber ducky, because I keep finding solutions for my problems like right after I post here '^^
swapped the if statement for @if (String.IsNullOrEmpty(item.PictureLink))
works
I will have another go, this time I'm properly out of ideas
My create function adds the picture fine, but for some reason the Edit doesn't work?
Here's the controller for Edit: