Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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.

Solution 2:

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";
}

Here are some more MS Office 2007 MIME types you may consider and on All MIME Types you may find a broader list.

Solution 3:

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

EDIT

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

Solution 4:

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

//on your view modelpublicclassMyViewModel
    {
        [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. publicclassFileUploadDBModel
    {
        [Key]
        publicint Id { get; set; }
        publicstring FileName { get; set; }
        publicbyte[] 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 controllerpublic ActionResult Index(MyViewModel model)
        {
                FileUploadDBModel fileUploadModel = new FileUploadDBModel();
                //uploading multiple files in the databaseforeach (var item in model.File)
                {
                    byte[] uploadFile = newbyte[item.InputStream.Length];
                    item.InputStream.Read(uploadFile, 0, uploadFile.Length);

                    fileUploadModel.FileName = item.FileName;
                    fileUploadModel.File = uploadFile;

                    db.FileUploadDBModels.Add(fileUploadModel);
                    db.SaveChanges();
                }
            }

Post a Comment for "Uploading Documents In Sql Server 2008 Using Asp.net C#"