How to save a PDF file to the database and make it available for download?

Asked

Viewed 1,161 times

3

I am developing a web application using Playframework, and I need a file that will be a kind of guide (documentation) for users who will use my application to be able to download or view by the browser itself in order to guide you in the handling of the platform. I’m using the local database for now, the H2 to store my data. How to save this pdf document in my bank so that it can be made available for download or viewing?

Here is one of the classes of the bank, where could be inserted here the pdf file.

@Entity
public class Auditoria extends Model{

public String usuario;
public String action;
public String controller;
@Temporal(TemporalType.TIMESTAMP)
public Date data;   

public Auditoria(){
    this.data = new Date();
}
}
  • 1

    Have you ever considered saving the file in a directory and saving in the bank only the path of it? It is a much better alternative.

  • No, I did not know of this alternative, can help me to implement?

  • 1

    I’m going home, when I get there I’ll come up with an answer for you.

  • @Diegoaugusto I stand by

  • You could save as blob in Base64 in the bank but @Carlosdiego’s proposal to save the directory path from it is much better

  • Can you help me implement this @Romulogabrielrodrigues?

  • Maybe only later because in my work I have no Java environment and I have no experience in this Play framework that is being used

  • Okay, I’m waiting @Romulogabrielrodrigues

  • Except in very specific cases, avoid saving files in the database, try to do by references, either directory, link or file name, otherwise you may incur performance problems in the transfer among others. <br>If you even need to do binary archiving, please follow:<br> https://answall.com/questions/100802/howto function- o-campo-blob <br> https://dev.mysql.com/doc/refman/5.7/en/binary-varbinary.html

Show 4 more comments

1 answer

-2


You can save any file to an sql database using a varbinary column(max).

Just create a method that converts the file to binary and save to DB, and then to read it just do the reverse.

  • Do you have any material or excerpt of code that might help me with that? I have no experience in that content, I’m learning now, so I don’t know where to start.

  • 1

    try to follow this: https://stackoverflow.com/questions/2579373/saving-any-file-to-in-the-database-just-convert-to-a-byte-array

  • Hello @Carlosdiego. Is it really necessary to implement this way? If not, I would suggest that you create an abstraction entity from the file and save it in format. pdf in some predefined directory, that way you would have an entity that contains the file and could access it without problems.

  • eh this from right @luanlucas, this way would solve my problem, you can help me implement this?

  • @Carlosdiego we can try. Is there any other way we can talk?

  • can be by wpp?

  • @luanlucas or so you can create a room here for us to talk?

  • @Carlosdiego, I created a room called "Create File Abstraction in Database". https://chat.stackexchange.com/rooms/71289/criar-abstracao-de-arquivo-em-banco-de-dados

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.