I will describe how to store files in SQL Server and Oracle. This largely depends on how you receive the file, primarily on how you get its contents, and on the database that you use for the content in which you will store it, and on how you will receive it keep. These are 2 separate database examples with 2 separate ways to get the file I used.
SQL Server
Short answer: I used the base64 byte string, which I converted to byte[] and saved in the varbinary(max) field.
Long answer:
Suppose you download through a website, so you use <input id="myFileControl" type="file"/> or React DropZone. To get the file, you do something like var myFile = document.getElementById("myFileControl")[0]; or myFile = this.state.files[0]; ,
From there I will get the base64 string, using the code here: Convert input = file to a byte array (use the UploadFile2 function).
Then I will get this line, file name ( myFile.name ) and type ( myFile.type ) in a JSON object:
var myJSONObj = { file: base64string, name: myFile.name, type: myFile.type, }
and xhr.send(JSON.stringify(myJSONObj); file to the MVC server side using XMLHttpRequest, specifying Content-Type application/json : xhr.send(JSON.stringify(myJSONObj); you need to create a ViewModel to associate it with:
public class MyModel { public string file { get; set; } public string title { get; set; } public string type { get; set; } }
and specify [FromBody]MyModel myModelObj as the passed parameter:
[System.Web.Http.HttpPost] // required to spell it out like this if using ApiController, or it will default to System.Mvc.Http.HttpPost public virtual ActionResult Post([FromBody]MyModel myModelObj)
You can then add this to this function and save it using the Entity Framework:
MY_ATTACHMENT_TABLE_MODEL tblAtchm = new MY_ATTACHMENT_TABLE_MODEL(); tblAtchm.Name = myModelObj.name; tblAtchm.Type = myModelObj.type; tblAtchm.File = System.Convert.FromBase64String(myModelObj.file); EntityFrameworkContextName ef = new EntityFrameworkContextName(); ef.MY_ATTACHMENT_TABLE_MODEL.Add(tblAtchm); ef.SaveChanges();
tblAtchm.File = System.Convert.FromBase64String(myModelObj.file); being an operational line.
You will need a model to represent the database table:
public class MY_ATTACHMENT_TABLE_MODEL { [Key] public byte[] File { get; set; } // notice this change public string Name { get; set; } public string Type { get; set; } }
This will save the data in the varbinary(max) field as byte[] . Name and Type were nvarchar(250) and nvarchar(10) , respectively. You can include the size by adding it to your table as an int & MY_ATTACHMENT_TABLE_MODEL as public int Size { get; set;} public int Size { get; set;} public int Size { get; set;} public int Size { get; set;} and add to the line tblAtchm.Size = System.Convert.FromBase64String(myModelObj.file).Length; above.
oracle
Short answer: convert it to byte[] , assign it to OracleParameter , add it to OracleCommand and update the table BLOB field using the link to the parameter value ParameterName :BlobParameter
Long answer: when I did this for Oracle, I used OpenFileDialog and extracted and sent the byte / file information as follows:
byte[] array; OracleParameter param = new OracleParameter(); Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); dlg.Filter = "Image Files (*.jpg, *.jpeg, *.jpe)|*.jpg;*.jpeg;*.jpe|Document Files (*.doc, *.docx, *.pdf)|*.doc;*.docx;*.pdf" if (dlg.ShowDialog().Value == true) { string fileName = dlg.FileName; using (FileStream fs = File.OpenRead(fileName) { array = new byte[fs.Length]; using (BinaryReader binReader = new BinaryReader(fs)) { array = binReader.ReadBytes((int)fs.Length); }
And inside the Oracle update done with ADO:
public void RunCommand(string sql, OracleParameter param) { OracleConnection oraConn = null; OracleCommand oraCmd = null; try { string connString = GetConnString(); oraConn = OracleConnection(connString); using (oraConn) { if (OraConnection.State == ConnectionState.Open) OraConnection.Close(); OraConnection.Open(); oraCmd = new OracleCommand(strSQL, oraConnection); // Add your OracleParameter if (param != null) OraCommand.Parameters.Add(param); // Execute the command OraCommand.ExecuteNonQuery(); } } catch (OracleException err) { // handle exception } finally { OraConnction.Close(); } } private string GetConnString() { string host = System.Configuration.ConfigurationManager.AppSettings["host"].ToString(); string port = System.Configuration.ConfigurationManager.AppSettings["port"].ToString(); string serviceName = System.Configuration.ConfigurationManager.AppSettings["svcName"].ToString(); string schemaName = System.Configuration.ConfigurationManager.AppSettings["schemaName"].ToString(); string pword = System.Configuration.ConfigurationManager.AppSettings["pword"].ToString(); // hopefully encrypted if (String.IsNullOrEmpty(host) || String.IsNullOrEmpty(port) || String.IsNullOrEmpty(serviceName) || String.IsNullOrEmpty(schemaName) || String.IsNullOrEmpty(pword)) { return "Missing Param"; } else { pword = decodePassword(pword); // decrypt here return String.Format( "Data Source=(DESCRIPTION =(ADDRESS = ( PROTOCOL = TCP)(HOST = {2})(PORT = {3}))(CONNECT_DATA =(SID = {4})));User Id={0};Password{1};", user, pword, host, port, serviceName ); } }
And the data type for the FILE_CONTENTS column was BLOB , FILE_SIZE was NUMBER(10,0) , LAST_MODIFIED was DATE , and the rest were NVARCHAR2(250) .