using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Net.Mail;
using System.Text;
using System.IO;
using System.Drawing;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// ArrayList fileList = (ArrayList)Session["Session"];
if (!IsPostBack)
{
// uploading files
Boolean fileOK = false;
String path = Server.MapPath("~/UploadedImages/");
if (FileUpload1.HasFile)
{
String fileExtension =
System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String[] allowedExtensions =
{ ".gif", ".png", ".jpeg", ".jpg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
}
}
}
if (fileOK)
{
try
{
FileUpload1.PostedFile.SaveAs(path
+ FileUpload1.FileName);
Label1.Text = "File uploaded!";
}
catch (Exception ex)
{
Label1.Text = "File could not be uploaded.";
}
}
else
{
Label1.Text = "Cannot accept files of this type.";
}
// checking if the folder exists and transfering
//the list offiles into an arraylist
ArrayList fileList = (ArrayList)Session["session"];
if (!Directory.Exists(Request.MapPath("Images")))
{
Directory.CreateDirectory(Request.MapPath("Images"));
}
Session["session"] = fileList;
ArrayList images = new ArrayList();
DirectoryInfo di = new DirectoryInfo("Images");
FileInfo[] rgFiles = di.GetFiles("*.jpg");
foreach (FileInfo fi in rgFiles)
{
string[] newarr = new string[5];
for (int i = 0, j = 0; i < rgFiles.Length; i++, j++)
{
newarr[j] = rgFiles[i].ToString();
}
images.Add(newarr);
Session["session"] = images;
// Response.Write("<br><a href=" + fi.Name + ">" + fi.Name + "</a>");
}
//ArrayList images = new ArrayList();
//DirectoryInfo di = new DirectoryInfo("Images");
//ArrayList files = di.GetFiles();
//ArrayList fileList = (ArrayList)Session["Session"];
}
Response.Write(((ArrayList)Session["Session"]).Count);
}
protected void btnEmail_Click(object sender, EventArgs e)
{
ArrayList img = (ArrayList)Session["session"];
MailMessage mail = new MailMessage("poopinder@gmail.com",
txtEmail.Text, "Email Message", " This message is sent using .net");
Attachment atch1;
atch1 = new Attachment(Request.MapPath("Images"));
mail.Attachments.Add(atch1 + img[i]);
// atch2 = new Attachment(Request.MapPath("email.aspx"));
// mail.Attachments.Add(atch2);
mail.BodyEncoding = Encoding.ASCII;
mail.IsBodyHtml = false;
// I am using IIS SMTP service . You can use your ISP email if allowed.
SmtpClient client = new SmtpClient("127.0.0.1");
// Add credentials if the SMTP server requires them.
// You can use an smtp server even if you have SMTP service ' installed on your machine
try
{
client.Send(mail);
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
protected void btnPrevious_Click(object sender, EventArgs e)
{
if (Directory.Exists(Request.MapPath("Images")))
{
// DirectoryInfo di = new DirectoryInfo("Images");
//String obj = new string(di);
// obj.IndexOf(0);
//if dir exists then load it to an obj
//then get the index of the obj
//which will indicate to the current index of the current file
//for (int i = 0; i < di.GetFiles().Length; i--)
//{ }
}
}
protected void btnUpload_Click(object sender, EventArgs e)
{
HttpPostedFile file = File1.PostedFile;
// extract the file name only and ignore the rest of the path
string fileName = file.FileName.Substring(file.FileName.LastIndexOf("\\") + 1);
System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream);
Bitmap bitmap = new Bitmap(img, img.Width, img.Height);
try
{
bitmap.Save(Request.MapPath(fileName), img.RawFormat);
Response.Write("file is saved");
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
}