To upload a docx file use below code,
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile && FileUpload1.PostedFile != null)
{
int ilength =
Convert.ToInt32(FileUpload1.PostedFile.ContentLength);
string content = FileUpload1.PostedFile.ContentType;
string file_name = FileUpload1.FileName;
Byte[] bytecontent = new Byte[ilength];
con = new SqlConnection();
con.ConnectionString = "Server=sqlserver;
Database=master; Trusted_Connection=True";
con.Open();
string sql = "insert into
files(file_data,name,content_type,file_size)VALUES(@file, @name, @content,
@size)";
try
{
SqlCommand cmd = new SqlCommand(sql, con);
FileUpload1.PostedFile.InputStream.Read(bytecontent, 0,
FileUpload1.PostedFile.ContentLength);
cmd.Parameters.AddWithValue("@file", bytecontent);
cmd.Parameters.AddWithValue("@name", file_name);
cmd.Parameters.AddWithValue("@content", content);
cmd.Parameters.AddWithValue("@size", ilength);
int i = cmd.ExecuteNonQuery();
TextBox1.Text = i.ToString();
}
catch (Exception ex)
{
TextBox1.Text = ex.Message;
}
finally
{
con.Close();
}
}
To display it you have to add
<a href="your file path" runat="server" id="filedisplay" />