-1

I have made an application which allows me to save a file temporarily as a bunch of bytes in an array. I have this available to me in C# as entity.FileUpload

I have another entity called: entity.Type which specifies if it is a PDF, DOCX, XLSX and so on... but for the sake of the question, ill only use PDF's

now from this stage, I want to be able to:

  1. convert the entity.FileUpload from its bunch of bytes, to a PDF and then...
  2. Save it on my computer under "C:/Temp";

So my question is: is it possible to convert this byte to a pdf, and then download it locally to my machine? and if yes how would I go about achieving this

If I require different blocks of code for different file types then that is ok, I can progress onto that at a later date, thankyou for any help on this matter

what data I can see under entity.FileUpload: enter image description here

How the file is saved:

I am using Javascript on the front end to save to a local database within the application as shown below:

  msls.readFile(screen, '.pdf', function (file, data) {

        var pdf = myapp.activeDataWorkspace.LocalDatabase.Enquiries.addNew();

        pdf.FileName = file.name;
        pdf.Type = file.type;
        pdf.FileUpload= data;

        myapp.activeDataWorkspace.LocalDatabase.saveChanges().done(function () {
            msls.showMessageBox('PDF ' + file.name + ' imported.');
        }, function (error) {
            msls.showMessageBox(error[0].message, { title: "Save Changes Error" });
        });
    });
  • the local database is for testing purposes called "LocalDatabase"
  • the table is called "Enquiries"
Crezzer7
  • 2,265
  • 6
  • 32
  • 63
  • how do you save the binary string into the database in the first place? that might help understand how to revert the process – Innat3 Oct 12 '16 at 09:59
  • Please have a look at [this answer](http://stackoverflow.com/a/381529/3581917) - does that help at all? You will need to use the `entity.Type` to determine the path (and name of the file), but that shouldn't be too hard. – Evil Dog Pie Oct 12 '16 at 10:01
  • 1
    The question is what you mean by "convert". Does the byte array contain the contents of a PDF file or does it contain something else? – Lasse V. Karlsen Oct 12 '16 at 10:01
  • for the sake of this question, I have uploaded a PDF file in which I want to save in a specific location on my hard drive. Im not sure if I need to convert it back to a PDF or if I can save it without doing this – Crezzer7 Oct 12 '16 at 10:05
  • Maybe I am blind, but I did not see any binary **string**. All I see is a bunch of bytes in an array. So where is that **string** mentioned in the question? – Sir Rufo Oct 12 '16 at 10:06
  • `File.WriteAllBytes(entity.FileName, entity.FileUpload);` (or something similar) should work fine. – Evil Dog Pie Oct 12 '16 at 10:08
  • Sir Rufo: thats the PDF file that has been uploaded in the debugging stage, I have specified the file type on the local database as a VARBINARY, – Crezzer7 Oct 12 '16 at 10:08
  • @Crezzer7 But that file content is just binary data => bunch of bytes => array of byte. No string at all – Sir Rufo Oct 12 '16 at 10:10
  • @Mike of SST, how would I go about saving that after onto my local drive? – Crezzer7 Oct 12 '16 at 10:12
  • @Crezzer7 That's what the `File.WriteAllBytes` method does. You tell it which file to write the data to using the (first) path parameter and which data to write using the (second) bytes parameter. Build the name of the file like `string path = @"C:\Temp\" + entity.FileName;`, or whatever works for you. (Assuming this is running on your local machine and it has permission to write to the local drive.) – Evil Dog Pie Oct 12 '16 at 10:17

1 Answers1

2

Try something like this:

        SqlCommand cmd;
        SqlConnection conn;
        SqlDataReader dr;
        string FileName = "";
        string FileType = "";

        //Get FileName And Type
        cmd = new SqlCommand("SELECT FileName, FileType FROM YourTable WHERE YourCondition", conn);
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            FileName = dr["FileName"].ToString();
            FileType = dr["FileType"].ToString();
        }

        //Load the varbinary into a datatable
        cmd = new SqlCommand("SELECT FileData FROM YourTable WHERE YourCondition", conn);
        DataTable dt = new DataTable();
        dr = cmd.ExecuteReader();
        dt.Load(dr);    

        //Extract the rows into a byte array
        byte[] stream = (byte[])dt.Rows[0][0];

        //Re-create the file from the byte array
        File.WriteAllBytes(@"C:\Temp\" + FileName + FileType, stream);
Innat3
  • 3,561
  • 2
  • 11
  • 29
  • You're better off using `Path.Combine()` than string concatenation. – Evil Dog Pie Oct 12 '16 at 10:25
  • This is only a sample that must be edited, I just want to make it as simple and understandable as possible – Innat3 Oct 12 '16 at 10:28
  • thankyou for your answer, Im actually only using the bottom 2 lines as I can intercept it before uploading into the database :) but thankyou for your answer, it works – Crezzer7 Oct 12 '16 at 10:28
  • np @Crezzer7, it was simpler than it seemed at first, if only you had phrased your question more clearly! – Innat3 Oct 12 '16 at 10:29
  • it certainly was, I didn't quote understand how the file was being stored temporarily which is why I ammended the question so many times, as people where asking question in the comments it became more clear! I get it now and have adapted the question to suite :) so at least its no longer a duplicate – Crezzer7 Oct 12 '16 at 10:32