using System; using System.Net; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; using System.IO; public partial class downloadprescription : System.Web.UI.Page { SqlConnection con = new SqlConnection("Data Source=DESKTOP-9RP88PP;Initial Catalog=Project;Integrated Security=True"); protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { Label2.Visible = true; string filePath = FileUpload1.PostedFile.FileName; // getting the file path of uploaded file string filename1 = Path.GetFileName(filePath); // getting the file name of uploaded file string ext = Path.GetExtension(filename1); // getting the file extension of uploaded file string type = String.Empty; if (!FileUpload1.HasFile) { Label2.Text = "Please Select File"; //if file uploader has no file selected } else if (FileUpload1.HasFile) { try { switch (ext) // this switch code validate the files which allow to upload only PDF file { case ".pdf": type = "application/pdf"; break; } if (type != String.Empty) { connection(); 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 string query = "insert into PDFFiles (Name,type,data)" + " values (@Name, @type, @Data)"; //insert query SqlCommand cmd = new SqlCommand(query, con); cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename1; cmd.Parameters.Add("@type", SqlDbType.VarChar).Value = type; cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes; cmd.ExecuteNonQuery(); Label2.ForeColor = System.Drawing.Color.Green; Label2.Text = "File Uploaded Successfully"; } else { Label2.ForeColor = System.Drawing.Color.Red; Label2.Text = "Select Only PDF Files "; // if file is other than speified extension } } catch (Exception ex) { Label2.Text = "Error: " + ex.Message.ToString(); } } } }
connection
txtFilename
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)