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;
public
partial class Default2 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data
Source=YESTK-PC;Initial Catalog=test;Integrated Security=True");
protected void Page_Load(object sender,
EventArgs e)
{
}
protected void Button1_Click(object sender,
EventArgs e)
{
// Read the file and convert it to Byte
Array
string filePath =
FileUpload1.PostedFile.FileName;
string filename =
Path.GetFileName(filePath);
string ext =
Path.GetExtension(filename);
string contenttype = String.Empty;
//Set the contenttype based on File
Extension
switch (ext)
{
case ".doc":
contenttype =
"application/vnd.ms-word";
break;
case ".docx":
contenttype =
"application/vnd.ms-word";
break;
case ".xls":
contenttype =
"application/vnd.ms-excel";
break;
case ".xlsx":
contenttype =
"application/vnd.ms-excel";
break;
case ".jpg":
contenttype = "image/jpg";
break;
case ".png":
contenttype =
"image/png";
break;
case ".gif":
contenttype =
"image/gif";
break;
case ".pdf":
contenttype =
"application/pdf";
break;
}
if (contenttype != String.Empty)
{
con.Open();
Stream fs =
FileUpload1.PostedFile.InputStream;
BinaryReader br = new
BinaryReader(fs);
Byte[] bytes =
br.ReadBytes((Int32)fs.Length);
//insert the file into database
SqlCommand cmd = new
SqlCommand("insert into upload(name, filetype, data) values (@name,
@filetype, @data)",con);
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value =
filename;
cmd.Parameters.Add("@filetype", SqlDbType.VarChar).Value
= contenttype;
cmd.Parameters.Add("@data", SqlDbType.Binary).Value = bytes;
//SqlDataReader dr =
cmd.ExecuteReader();
//if (dr.Read())
//{
Response.Write("saved");
//}
cmd.ExecuteNonQuery();
Label1.ForeColor = System.Drawing.Color.Green;
Label1.Text = "File Uploaded
Successfully";
con.Close();
}
else
{
Label1.ForeColor =
System.Drawing.Color.Red;
Label1.Text = "File format not
recognised." +
" Upload
Image/Word/PDF/Excel formats";
}
}
}
No comments:
Post a Comment