Save/Recover Files with PDF Extension

Asked

Viewed 476 times

1

To save files with extension PDF in the database it is necessary to do the conversion to base 64? Or would there be some other way to save this type and its recovery for viewing?

The code I have below does the image conversion to base 64, where image recovery is properly handled. While saving a PDF file it converts the same, but in file recovery gets like this: inserir a descrição da imagem aqui

using (var stream = new MemoryStream())
               {
                clientesFotos.File.CopyTo(stream);
                System.IO.File.WriteAllBytes(path, stream.ToArray());
                clientesFotos.DocumentoString = Convert.ToBase64String(stream.ToArray());
                stream.Dispose();
                stream.Close();
                 }

Return of converted files from base 64

 function exibirDocumentos(img) {
                //$("#overlay").show();
                $("#imagem").attr("src", "data:image/jpeg;base64," + document.getElementById(img).value);
            }
  • The file extension is a reference for the end user and for the Operating System. The computer cares about the actual content in the file. For example, you can create a text file, write your name on it and save it with the name and extension "my_photo.jpg", but this will not make your text file into an image. At most the System will try to treat your text file as a photo and will fail. In your case, the binary content of the file is a image or a PDF?

  • You are saving the file in the database at Base64?

  • @Rodrigok. B Yes! But in recovery for viewing it, it does not display. Unlike jpeg, png... ?

  • I think your problem isn’t conversion, it’s how this Base64 is doing. This c# code of your question, do you use it to retrieve the file for download, or to save in the database? If possible post a stirng Base64 of some test file of yours, exactly how is saving in the database.

  • It makes no sense to want to put a PDF on the page as if it were an image. The problem has nothing to do with base 64 or storage.

2 answers

4


If you are saving as PDF, you cannot display with content-type image/jpeg.

To display as image/jpeg, you first have to convert the PDF to jpeg.

  • The content-type is passing as "application/pdf" but is being converted to base 64, my question is whether this is a rushed way to work as pdf data storage

0

The best way to save files, is to save on hard drive.

But depending on the database, you have the option to save as varbinary(max) (FILESTREAM or BLOB), not requiring conversion, being able to directly save bytes in the database. When saving or recovering, conversion to BASE64 is waived.

Browser other questions tagged

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