using System.Data;
using System.IO;
public partial class _Default :
System.Web.UI.Page
{
public void
SaveFilePath(string name, int size, string
path)
{
string insertText = "INSERT
INTO Table1(FileName, FileSize, FilePath) VALUES (@FileName, @FileSize,
@FilePath)";
string myConnection = "Data
Source=Local;Initial Catalog=FilesDatabase;Integrated Security=True";
SqlConnection connection = new SqlConnection(myConnection);
SqlCommand command = new
SqlCommand(insertText, connection);
command.Parameters.Add("@FileName",
SqlDbType.VarChar, 50);
command.Parameters["@FileName"].Value
= name;
command.Parameters.Add("@FileSize",
SqlDbType.Int);
command.Parameters["@FileSize"].Value
= size;
command.Parameters.Add("@FilePath",
SqlDbType.VarChar, 300);
command.Parameters["@FilePath"].Value
= path;
try
{
connection.Open();
command.ExecuteNonQuery();
}
catch (Exception
ex)
{
Label1.Text = ".";
}
finally
{
connection.Close();
}
}
protected void
Button1_Click(object sender, EventArgs e)
{
bool correctExtension = false;
if (FileUpload1.HasFile)
{
string fileName = FileUpload1.PostedFile.FileName;
int fileSize = FileUpload1.PostedFile.ContentLength;
string fileExtension = Path.GetExtension(fileName).ToLower();
string[] extensionsAllowed = { ".doc", ".docx",
".xls", ".xlsx",
".txt", ".jpg",
".png" };
if (extensionsAllowed.Contains(fileExtension))
{
correctExtension = true;
}
if (correctExtension)
{
if (fileExtension == ".docx" || fileExtension == ".txt" || fileExtension == ".xlsx")
{
try
{
string filePath = Server.MapPath("~/Files/Documents/"
+ fileName);
FileUpload1.PostedFile.SaveAs(filePath);
SaveFilePath(fileName,
fileSize, filePath);
Label1.Text = "Amjilttai bolloo";
}
catch (Exception
ex)
{
Label1.Text = "Amjiltgvi";
}
}
else if (fileExtension == ".jpg"
|| fileExtension == ".png")
{
try
{
string filePath = Server.MapPath("~/Files/Images/"
+ fileName);
FileUpload1.PostedFile.SaveAs(filePath);
SaveFilePath(fileName,
fileSize, filePath);
Label1.Text = "Amjilttai bolloo";
}
catch (Exception
ex)
{
Label1.Text = "Amjiltgvi";
}
}
}
else
{
Label1.Text = "Zurag Document
oruul" + fileExtension + "...upload
hiih bolomjgvi";
}
}
}
}