Uploading documents in sql server 2008 using asp.net C#


I'm currently researching different methods and techniques into uploading and downloading documents into a sql server 2008 database using ASP.Net and C# for a web application.

I have found this <a href="http://www.c-sharpcorner.com/UploadFile/0c1bb2/uploading-and-downloading-excel-files-from-database-using-as/" rel="nofollow">excel tutorial</a>, and i was wondering if it would be similar to upload pdf and word documents in a similar way?



This tutorial should work for any file, not just excel. The key is in this part:

Stream fs = FileUpload1.PostedFile.InputStream; BinaryReader br = new BinaryReader(fs); //reads the binary files Byte[] bytes = br.ReadBytes((Int32)fs.Length); //counting the file length into bytes query = "insert into Excelfiledemo(Name,type,data)" + " values (@Name, @type, @Data)"; //insert query com = new SqlCommand(query, con); com.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename1; com.Parameters.Add("@type", SqlDbType.VarChar).Value = type; com.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes; com.ExecuteNonQuery(); Label2.ForeColor = System.Drawing.Color.Green; Label2.Text = "File Uploaded Successfully";

What is basically happening here is that the file stream is being turned into a Byte array which is stored as a data blob in your database. This can be used for ANY file type. Just be sure to keep the filename (or at least extension) around just as in the example above so that you know what kind of file it is when you turn it back into a file on disk.


Considering the example in the link you shared, use the following validation:

switch (ext.ToLower()) { case ".pdf": type = "application/pdf"; break; case ".doc": type = "application/msword"; break; case ".docx": type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; }

<a href="http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/05/08/office-2007-open-xml-mime-types.aspx" rel="nofollow">Here</a> are some more MS Office 2007 MIME types you may consider and on <a href="http://www.fileformat.info/info/mimetype/all.htm" rel="nofollow">All MIME Types</a> you may find a broader list.


The answer is simple: YES, it's similar.


The link you found is a sample shows you how to store a file content into database, it can be changed to any other different file type: pdf,doc,jpg, whatever, as long as you record the mim e type so that when download end user get it correctly


This answer is more appropriate when you are using ASP.NET MVC 4 or 5 with code first.

//on your view model public class MyViewModel { [Required] [DisplayName("Select File to Upload")] public IEnumerable<HttpPostedFileBase> File { get; set; } } // on your object class, make sure you have a data type byte[] that will store a file in a form of bytes, a byte[] data type store both Images and documents of any content type. public class FileUploadDBModel { [Key] public int Id { get; set; } public string FileName { get; set; } public byte[] File { get; set; } } // on your view <div class="jumbotron"> @using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" })) { <div> @Html.LabelFor(x => x.File) @Html.TextBoxFor(x => x.File, new { type = "file" }) @Html.ValidationMessageFor(x => x.File) </div> <button type="submit">Upload File</button> } </div> //on your controller public ActionResult Index(MyViewModel model) { FileUploadDBModel fileUploadModel = new FileUploadDBModel(); //uploading multiple files in the database foreach (var item in model.File) { byte[] uploadFile = new byte[item.InputStream.Length]; item.InputStream.Read(uploadFile, 0, uploadFile.Length); fileUploadModel.FileName = item.FileName; fileUploadModel.File = uploadFile; db.FileUploadDBModels.Add(fileUploadModel); db.SaveChanges(); } }


  • Getting intersection of two lists in python
  • Multiple lines of text in UISegmentedControl
  • Jenkinsfile - agent matching multiple labels
  • Clicking checkbox causes onCheckedChanged to fire twice
  • JavaFX ContextMenu how do I get the clicked Object?
  • How to merge two sets of weka Instances together
  • How to show created ToolTipControl from ToolTip Component?
  • PHP Regexp on filename and number - preg_replace_callback
  • Access VBA If statement to Change Back and Font color of a report field
  • How can I change the ForeColor of the GroupBox text without changing its children ForeColor?
  • Getting value from radiobox in Tkinter - Python
  • Add apostrophe character to Excel export with OleDB connection
  • How to execute a shell script through python
  • How to insert data into a Microsoft Access Database?
  • Using SQL stored function gives invalid SQL statement
  • Get attributes of existing SVG elements and bind as data with d3.js
  • Setting color in a row of a Jtable
  • Argument 5: cannot convert from 'System.Drawing.Image' to 'string' - calling cla
  • UIButton Borders Function Only Gives Back White Borders
  • Backward compatibility of Python 3.5 for external modules
  • Hibernate to update table schema
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • Zoom in and out of jPanel
  • Graphics.CopyFromScreen [Web application] + The handle is invalid
  • Debug.DrawLine not showing in the GameView
  • Abort upload large uploads after reading headers
  • How to modify the colors in the legend of a plot using a fill gradient?
  • MySQL WHERE-condition in procedure ignored
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • How do you troubleshoot character encoding problems?
  • Web-crawler for facebook in python
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • trying to dynamically update Highchart column chart but series undefined
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • coudnt use logback because of log4j
  • How does Linux kernel interrupt the application?
  • java string with new operator and a literal