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:
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?
– Diego Rafael Souza
You are saving the file in the database at Base64?
– Rodrigo K.B
@Rodrigok. B Yes! But in recovery for viewing it, it does not display. Unlike jpeg, png... ?
– Igor Carreiro
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.
– Rodrigo K.B
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.
– Bacco