You do not prevent it. The DBMS and only the DBMS is responsible to creating the Primary Key values and maintaining it's constraints. Doing this in your Client is just going to fail horribly. You will run head first into a race condition that way.
If you do need the Key of something you just inserted, SQL actually has the OUTPUT Syntax. I remember other DBMS not having something comparable, however. But usually you can make something using a stored funciton equivalent and a wide enough transaction/lock.
As a side note, it is worth mentioning that there are 3 ways to save BLOBS with/in DBs. I can not really tell wich of the 3 you are using.