how to store a Jpeg file in a database? - database

Is it possible to store a Jpeg file in a database using a Web Application?
can you share some code on how to do it?
or share some links for some tutorial regarding with my problem? thanks a lot :)

storing an image in a blob
i don't use java, i don't know what database structure you are using but the principle is to store the image in a binary large object. (blob) it does require reading the object in and out using streams. (or at least in c# this is the case)
the alternative(if it works) is to store a string location and have the image in a directory somewhere.

BLOB data type, that is what u need.
and the link is ;)
blob google search

This is not a good practice to store a jpeg file in database. Always store the file in related directories and store their paths in your database. If you are storing jpeg files in your database, then think about the size of your database if you are having 1 million records?

Related

How to insert and retrieve picture in sqlite database in CN1

I have challenges in fetching picture saved as bytes in sqlite database in codename one. Pls is there any special way in codename one to get picture from the sqlite database?
I am using this code
byte[] img = r.getBlob(0);
pls is their anythng wrong here?. I need a snippet code that will fetch pictures from the database. Bcos my pictures must reside inside database for better calling. Thanks
I would avoid that and instead store the data as a file as the blob functionality is flaky across platforms. Save the path to the file (or its name) in sqlite and store the files in storage or filesystem.

Server backend: how to generate file paths for uploaded files?

I am trying to create a site where users can upload images, videos and other types of files.
I did some research and people seem to suggest that saving the files as BLOB in database is a Bad idea; instead, save the file paths in database.
My questions are, if I save the file paths in a database:
1. How do I generate the file names?
I thought about computing the MD5 value of the file name, but what if two files have the same name? Adding the username and time-stamp etc. to file name? Does it even make sense?
2. What is the best directory structure?
If a user uploads images at 12/17/2013, 12/18/2018, can I just put it in user_ABC/images/, then create time-stamped sub-directories 20131217, 20131218 etc. ? What is the best structure for all these stuff?
3. How do all these come together?
It seems like maintaining this system is such a pain, because the file system manipulation scripts are tightly coupled with the database operations(may also need the worry about database transactions? Say in one transaction I updated the database but failed to modify the file system so I need to roll back my database?).
And I think this system doesn't scale (what if my machine runs out of hard disk so I need to upload the files to a second machine? What if my contents are on a cluster?)
I think my real question is:
4. Is there any existing framework/design pattern/db that handles this problem?
What is the standard way of handling this kind of problems?
Thanks in advance for your answers.
I've actually asked this same question when I was designing a social website for food chefs. I decided to store the url of the image in a MySQL database along with recipe. If you plan on storing multiple images for one recipe, in my example, maybe having a comma separated value would work. When the recipe loaded on the page, I would fetch the image associated with that recipe onto the screen.
Since it was a hackathon and wasn't meant for production purposes, I didn't encode the file name into something unique. However, if I were developing for productional purposes, I would append the time-stamp to the media file name when storing it into the server and database/backend.
I believe what I've proposed is the best data structure of handling this scenario. Storing the image onto the server is not only faster, but it should also take less space. I have found that when converting a standard jpg file of reasonable resolution to base64 encoding, the encoded text file representation took 30% more space. There is also the time of encoding the file and decoding the file for storage and resolving when using some BLOB type of data format instead of straight up storing the file on the server.
Using some sort of backend server scripting like PHP, you'll be able to do some pretty neat stuff with the information you have available. Fetch the result from the database, and load it in from the page using HTML.
As far as I know, there isn't a standard way of fetching media from a database yet. Perhaps there will be one day.
There is not standard way to do that, it is different to the different application. The idea is you need generate a different Path+FileName for every upload, here is a way:
HashId = sha1(microsecond + random(1,1000000));
Path = /[user_id]/[HashId{0,2}]/[HashId{-2}];
FileName = HashId

Insert a image in imageview with url into sqlite DB without BLOB

I need to insert an image into a imageview with url into sqlite DB without using the BLOB, is possible??
Thanks in advance
Short answer: I am not aware of any way to save images in SQLite without using BLOBs, and BLOBs of images can be too large for the server to handle smoothly. But I won't claim to have thorough experience with SQLite, so there's a good chance I'm missing something.
Depending on what other resources are at your disposal, one solution would be to just store the url for the image in a TEXT type, but save the image in some other file system. Then when you want to display the image, query the database to get the address (plus whatever other relevant info), isolate it from the results, and query the file system for the image. Implementing this pattern will really depend on the environment your system will be working in; if it's an Android device, you can probably just refer to the filepath it started at, or save it to a dedicated folder; if it's a web interface, the images would most easily be saved as a sub-folder next to the web page(s) that use the images.

grails file upload

Hey. I need to upload some files (images/pdf/pp) to my SQLS Database and thereafter, download it again. I'm not sure what is the best solution - store it as bytes, or store it as file (not sure if possible). I need later to databind multiple domain classes together with that file upload.
Any help would be very much apreciated,
JM
saving files in the file system or in the DB is a general question which is asked here several times.
check this: Store images(jpg,gif,png) in filesystem or DB?
I recommend to save the files in the file system and just save the path in the DB.
(if you want to work with google app-engine though you have to save the file as byte array in the DB as saving files in the file system is not possible with google app-engine)
To upload file with grails check this: http://www.grails.org/Controllers+-+File+Uploads

What is the best format to store images in a database?

What is the best format to store images in a database, such as binary,base64...etc, for optimal speed/size.
Microsoft's advice for SQL Server used to be, for speed and size, store images in the file system, with links in the database. I think they've softened their preference a bit, but I still consider it a better idea certainly for size, since it will take up no space in the database.
Blob is the answer for your question.
Instead of storing image to database directly you can store the image in hard disk And store the filename in database.I think this way is appropriate.Performance wise this is good.
Binary.
Other than that, it's up to the uses of the images (quality, compression, etc.) in terms of whether you choose PNG, GIF, JPEG, TIFF, etc.
The use case will (should) drive the format more than the database.
Most databases can store binary data in some way:
MySQL: BLOB;
Oracle: BLOB;
SQL Server: binary, varbinary and image.
As for the image file format, that is a separate issue. Not sure if you were alluding to that problem. If you were, it usually doesn't matter between PNG, GIF and JPG other than JPG being a lossy format (the other two are lsosless), which is OK for photos and the like but not, say, for icons or text.
Blob's don't care what kind of image you fill them with, so really, the right choice is probably whatever requires the least post select processing before its used.
Use hdf5.
Similar to XML documents, HDF files are self-describing and allow users to specify complex data relationships and dependencies. In contrast to XML documents, HDF files can contain binary data (in many representations) and allow direct access to parts of the file without first parsing the entire contents.
That depends alot on what the images are used for.
Avatars can just be compressed JPEGS
Images that will be manipulated via image processing software would be TIFF
Images on a photo site would be JPEG for viewing, TIFF for downloading.
We just need a bit more information. If the files are very large, store them in a directory with a link in the database.

Resources