You shouldn't be using the BinaryFormatter for this - that's for serializing .Net types to a binary file so they can be read back again as .Net types.
If it's stored in the database, hopefully, as a varbinary - then all you need to do is get the byte array from that (that will depend on your data access technology - EF and Linq to Sql, for example, will create a mapping that makes it trivial to get a byte array) and then write it to the file as you do in your last line of code.
With any luck - I'm hoping that fileContent here is the byte array? In which case you can just do
System.IO.File.WriteAllBytes("hello.pdf", fileContent);
Answer from Andras Zoltan on Stack Overflowc# - byte array to pdf - Stack Overflow
how to convert byte array to pdf and download
Convert from byte array to pdf file - Free Support Forum - aspose.com
Converting byte Array to PDF - Storage & SAN - Spiceworks Community
How do I convert a byte array into a PDF file using C#?
ChromePdfRenderer class. First, convert the byte array to a string using ASCII encoding, create an HTML string, and then render it to PDF using IronPDF.How can I convert a byte array to a PDF in Java?
renderHtmlAsPdf method to convert the HTML to a PDF document.How can I ensure correct encoding when converting byte arrays to strings in C#?
Videos
You shouldn't be using the BinaryFormatter for this - that's for serializing .Net types to a binary file so they can be read back again as .Net types.
If it's stored in the database, hopefully, as a varbinary - then all you need to do is get the byte array from that (that will depend on your data access technology - EF and Linq to Sql, for example, will create a mapping that makes it trivial to get a byte array) and then write it to the file as you do in your last line of code.
With any luck - I'm hoping that fileContent here is the byte array? In which case you can just do
System.IO.File.WriteAllBytes("hello.pdf", fileContent);
Usually this happens if something is wrong with the byte array.
File.WriteAllBytes("filename.PDF", Byte[]);
This creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten.
Asynchronous implementation of this is also available.
public static System.Threading.Tasks.Task WriteAllBytesAsync
(string path, byte[] bytes, System.Threading.CancellationToken cancellationToken = null);
The string from the response seem to be Base-64 encoded (ref. fiddle). You can decode it using fetch() (or XMLHttpRequest() for older browsers):
fetch("data:application/pdf;base64," + response.data)
.then(function(resp) {return resp.blob()})
.then(function(blob) {
FileSaver.saveAs(blob, 'foo.pdf')
});
If anyone looking for solution with same problem, here is the nice article with cross browser support.
Snippet from article:
const binaryString = window.atob(fileResponseData); const bytes = new Uint8Array(binaryString.length); const mappedData = bytes.map((byte, i) => binaryString.charCodeAt(i)); const blob = new Blob([mappedData], { type: 'application/pdf' }); FileSaver.saveAs(blob, 'foo.pdf')