byte array to pdf

前端 未结 2 458
北恋
北恋 2020-12-29 02:25

I am trying to convert content of a file stored in a sql column to a pdf.

I use the following piece of code:

byte[] bytes;
BinaryFormatter bf = new B         


        
相关标签:
2条回答
  • 2020-12-29 02:40

    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);
    
    0 讨论(0)
  • 2020-12-29 02:46

    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);
    
    0 讨论(0)
提交回复
热议问题