Step 1:- Table Script
CREATE TABLE testTable
(
ID INT IDENTITY,
TEXT_DATA TEXT,
XML_DATA XML,
IMG_DATA IMAGE
)
Step 2:- Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="
Default.aspx.cs" Inherits="Demo.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function Validate()
{
var FileUploadText
= document.getElementById("FileUploadText").value;
var FileUploadXML
= document.getElementById("FileUploadXML").value;
var FileUploadIMG
= document.getElementById("FileUploadIMG").value;
if (FileUploadText
=== "") {
alert("Select the text file.");
document.getElementById("FileUploadText").focus();
return false;
}
else {
var ext
= FileUploadText.split(".")[1];
if (ext.toLowerCase()
!== "txt") {
alert("Only .txt file allowed.");
document.getElementById("FileUploadText").focus();
return false;
}
}
if (FileUploadXML
=== "") {
alert("Select the XML File.");
document.getElementById("FileUploadXML").focus();
return false;
}
else {
var ext
= FileUploadXML.split(".")[1];
if (ext.toLowerCase()
!== "xml") {
alert("Only .xml file allowed.");
document.getElementById("FileUploadText").focus();
return false;
}
}
if (FileUploadIMG
=== "") {
alert("Select the Image.");
document.getElementById("FileUploadIMG").focus();
return false;
}
else {
var ext
= FileUploadIMG.split(".")[1];
if (ext.toLowerCase()
!== "jpg") {
alert("Only .jpg file allowed.");
document.getElementById("FileUploadIMG").focus();
return false;
}
}
return true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="1">
<tr>
<td>Text
File</td>
<td>
<asp:FileUpload ID="FileUploadText" runat="server" ClientIDMode="Static" /></td>
</tr>
<tr>
<td>XML
File</td>
<td>
<asp:FileUpload ID="FileUploadXML" runat="server" ClientIDMode="Static" /></td>
</tr>
<tr>
<td>Image</td>
<td>
<asp:FileUpload ID="FileUploadIMG" runat="server" ClientIDMode="Static" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
Step 3:- Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Text;
namespace Demo
{
public partial class Default :
System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;
FileStream fs;
BinaryReader br;
StreamReader sr;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
btnSubmit.Attributes.Add("onclick", "return
Validate();");
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
//
Read Text File
fs = new FileStream(FileUploadText.PostedFile.FileName, FileMode.Open, FileAccess.Read);
sr = new StreamReader(fs);
string txt
= sr.ReadToEnd();
sr.Close();
fs.Close();
//
Read XML File
fs = new FileStream(FileUploadXML.PostedFile.FileName, FileMode.Open, FileAccess.Read);
sr = new StreamReader(fs);
string xml
= sr.ReadToEnd();
sr.Close();
fs.Close();
//
Read IMG
fs = new FileStream(FileUploadIMG.PostedFile.FileName, FileMode.Open, FileAccess.Read);
br = new BinaryReader(fs);
byte[] img
= br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
string connection
= "server=Server IP; database=Database Name;
uid=Server User Id; pwd=Server Password;";
con = new SqlConnection(connection);
con.Open();
string query
= "insert into
testTable(TEXT_DATA,XML_DATA,IMG_DATA)values(@TEXT_DATA,@XML_DATA,@IMG_DATA)";
cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
cmd.CommandText = query;
cmd.Parameters.AddWithValue("@TEXT_DATA",
txt);
cmd.Parameters.AddWithValue("@XML_DATA",
xml);
cmd.Parameters.AddWithValue("@IMG_DATA",
img);
int i
= cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
throw;
}
}
}
}
Note:-
Text File content
Test Data
Open notepad => copy Text file content =>
past it into file => save file as a test.txt.
XML
File content
<note>
<to>Raj</to>
<from>Jony</from>
<heading>Reminder</heading>
<body>Dont forget me this weekend!</body>
</note>
Open notepad => copy XML file content =>
past it into file => save file as a test.xml
For Image take .jpg extension image. In this demo
we are using .jpg image only.
Right click on image to save it.