Hi Mohammadmk,
Refer the below sample code.
C#
protected void OnUploadComplete(object sender, AjaxFileUploadEventArgs e)
{
string contentTypes = e.ContentType.ToLower();
string fileName = Path.GetFileName(e.FileName);
AjaxFileUpload11.SaveAs(Server.MapPath("~/Uploads/" + fileName));
string filePath = Server.MapPath("~/Uploads/" + fileName);
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand("INSERT INTO tblFiles(Name, ContentType, Data) values (@Name, @ContentType, @Data)");
cmd.Parameters.AddWithValue("@Name", fileName);
string contentType = "";
switch (contentTypes)
{
case ".pdf":
contentType = "Application/pdf";
break;
case ".doc":
contentType = "application/vnd.ms-word";
break;
case ".docx":
contentType = "application/vnd.ms-word";
break;
case ".xls":
contentType = "Application/x-msexcel";
break;
case ".xlsx":
contentType = "Application/pdf";
break;
case ".jpg":
contentType = "image/JPEG";
break;
case ".gif":
contentType = "image/GIF";
break;
default:
break;
}
cmd.Parameters.AddWithValue("@ContentType", contentType);
cmd.Parameters.AddWithValue("@Data", bytes);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
con.Dispose();
}
}
VB.Net
Protected Sub OnUploadComplete(sender As Object, e As AjaxFileUploadEventArgs)
Dim contentTypes As String = e.ContentType.ToLower()
Dim fileName As String = Path.GetFileName(e.FileName)
AjaxFileUpload11.SaveAs(Server.MapPath(Convert.ToString("~/Uploads/") & fileName))
Dim filePath As String = Server.MapPath(Convert.ToString("~/Uploads/") & fileName)
Dim fs As New FileStream(filePath, FileMode.Open, FileAccess.Read)
Dim br As New BinaryReader(fs)
Dim bytes As Byte() = br.ReadBytes(Convert.ToInt32(fs.Length))
Dim strConnString As [String] = System.Configuration.ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim con As New SqlConnection(strConnString)
Dim cmd As New SqlCommand("INSERT INTO tblFiles(Name, ContentType, Data) values (@Name, @ContentType, @Data)")
cmd.Parameters.AddWithValue("@Name", fileName)
Dim contentType As String = ""
Select Case contentTypes
Case ".pdf"
contentType = "Application/pdf"
Exit Select
Case ".doc"
contentType = "application/vnd.ms-word"
Exit Select
Case ".docx"
contentType = "application/vnd.ms-word"
Exit Select
Case ".xls"
contentType = "Application/x-msexcel"
Exit Select
Case ".xlsx"
contentType = "Application/pdf"
Exit Select
Case ".jpg"
contentType = "image/JPEG"
Exit Select
Case ".gif"
contentType = "image/GIF"
Exit Select
Case Else
Exit Select
End Select
cmd.Parameters.AddWithValue("@ContentType", contentType)
cmd.Parameters.AddWithValue("@Data", bytes)
cmd.CommandType = CommandType.Text
cmd.Connection = con
Try
con.Open()
cmd.ExecuteNonQuery()
Catch ex As Exception
Response.Write(ex.Message)
Finally
con.Close()
con.Dispose()
End Try
End Sub