✅ SQL and images
Hi, so, i made this thing (screenshots), and what should i do with images, i mean, which is most used way to deal with images?
44 Replies
What do you mean "deal with"?
As in, how to store them?
Store them in a CDN or at least the filesystem, and save their location in the database
file stream is the first thing that came to my mind, and how, is that ok?(just wanted to ask because sometimes you can do something and after someone tells you, there is something specifically for that and you could have done it much easier) ? or is it better to figure out how to work with this "cdn" thing?
Uh file stream is a way to read or write a file from/to a disk
So yes, you can use it to do that
so, like here, but instead of html file, im reading image and after putting that file in db
If you want to send the file manually, sure
Or use the static files middleware
got something like this,
Product class, generated by EFc.
and got this
(all images are same cuz default value)
stored in db as byte array
All of that after me telling you to not save images in the database, huh?
wait
don't rly get it
where should i save them if not in db?
oh
i remember
i mean, have seen
i need to save images in specific folders
and save path to them in db?
Yep
and when i need to send image, then i make byte array
right?
Well, depends how you want to send those images
Because if you use a form, or a form-style request, you can just use an
IFormFile
If not then yeah, I guess you can encode the image as Base64 and send it as a part of the JSON requestok, i got this thing and gonna rewrite code
btw, anyway topic about images is open, in case of adding product in db, im just getting byte array from request body, generating random name while(db.Count(p => p.Image == [randomly generated name]) == 0, and saving this image into images folder with this name. and after i save product in db, where Image is string and value is randomly generated name.
or maybe there is better ways to do that? @ZZZZZZZZZZZZZZZZZZZZZZZZZ
A randomly generated name is fine, yeah
You can use a GUID to make sure there are no collisions too
when i said [randomly generated name]. i meant guid (have seen some examples of it (but not overcommitted)), it still can generate same name, or no?
Yes, but the chances of that are incredibly unlikely
ok, thx closed to early 🙂 so, instead of sending data like this (first code), im writing new logic and sending list of anonyms objects or what (second code)? @ZZZZZZZZZZZZZZZZZZZZZZZZZ
or should i make whole new class, like, ProductToSent, and fill this class using info from prod object?
Ideally, yes, make a proper class (or a record at least) to describe the data
Actually, a record might be easier and more proper here
yeap, already started
finally got something like this
works well
.Result
this is a no-no
Also, not sure why you're reding the image into memory and then returning the bytes
Instead of using the static dile middleware and just returning the URLi just don't know what is it, lmao, ok looks like need to see what this thing do
It's a one line in your
Program.cs
that will make it so ASP will automatically serve all files from /wwwroot
directory
So if you have /wwwroot/images/cat.png
you'll be able to just have <img src="/images/cat.png" />
in your HTML and it'll workyeah, i know it, using for that thing
should i save all images here or what u mean?
ye
oh, i thought static files ar about sending some default things, like logos, animations ect, not for user data
and wanted to make server side folder of images
to send only required images
I mean, it will still send only the required image
oh, then it looks super easy
expect 1 thing
image type
Whatever
The middleware will choose the correct mime type
Unless you meant to ask about what's the best image type to use
In which case, AVIF -> WEBP -> PNG
i mean, i want to add image upload later
Yea
and users can upload different types
but looks like
Yep
i have to change type to what i want
You can, sure
or its not rly matter?
But you don't have to
ok, makes sanse
soooo
then
1 sec.
oh
nice
don't even need this preview class anymore
and i just store my image in static files folder
and that is?
ye
🤯
Thanks as always. thats rly saved my head from burnout