using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class example1004_upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//private string uploadDirectory = @"E:\";
protected void cmdUpload_Click(object sender, EventArgs e)
{
string SaveLocation = Server.MapPath("uploaded_images");
// Check that a file is actually being submitted.
if (FileInput.PostedFile.FileName == "") //FileInput is the asp:FileUpload tag
{
lblInfo.Text = "No file specified."; //lblInfo is the asp:Label tag
}
else
{
// Check the extension.
string extension = Path.GetExtension(FileInput.PostedFile.FileName);
switch (extension.ToLower())
{
case ".bmp":
case ".gif":
case ".jpg":
case ".pdf":
case ".tif":
break;
default:
lblInfo.Text = "This file type is not allowed.";
return;
}
//you may need the date and time for file naming purposes
DateTime myDate = DateTime.Now;
Response.Write (myDate.ToString()+"
");
string myYear = myDate.Year.ToString();
Response.Write(myYear+"
");
string myMonth = myDate.Month.ToString();
Response.Write(myMonth+"
");
string myDay = myDate.Day.ToString();
Response.Write(myDay+"
");
string myHour = myDate.Hour.ToString();
Response.Write(myHour+"
");
string myMinute = myDate.Minute.ToString();
Response.Write(myMinute+"
");
string mySecond = myDate.Second.ToString();
Response.Write(mySecond+"
");
string myMillisecond = myDate.Millisecond.ToString();
Response.Write(myMillisecond+"
");
//get original file name
string serverFileName = Path.GetFileName(
FileInput.PostedFile.FileName);
//find path to move file to
//string fullUploadPath = Path.Combine(uploadDirectory, serverFileName); //uploadDirectory declared above
string fullUploadPath = SaveLocation + "\\" + serverFileName;
try
{
// This overwrites any existing file with the same name.
// Use File.Exists() to check first if this is a concern.
FileInput.PostedFile.SaveAs(fullUploadPath);
lblInfo.Text = "File " + serverFileName;
lblInfo.Text += " uploaded successfully";
// lblInfo.Text += " to "+fullUploadPath;
File.Delete(fullUploadPath);
//delete the uploaded file, for server space issue
}
catch (Exception err)
{
lblInfo.Text = err.Message;
}
}
}
}