Home
Manage Your Code
Snippet: Simple File Upload Script (C#)
Title: Simple File Upload Script Language: C#
Description: Simple File Upload Script Views: 131
Author: Thomas Jenkins Date Added: 8/28/2008
Copy Code  
1bool hasfile = frmFileUploadPhoto.HasFile;
2
3		if (frmFileUploadPhoto.HasFile)
4		{
5			string extension = System.IO.Path.GetExtension(frmFileUploadPhoto.FileName).ToLower();
6
7			string[] filetypes = { ".jpg", ".gif", ".png", ".bmp" };
8
9			bool acceptableFile = false;
10			foreach (string type in filetypes)
11			{
12				if (extension == type)
13					acceptableFile = true;
14			}
15			if (acceptableFile)
16			{
17				FileInfo fi = new FileInfo(Path.GetFullPath(frmFileUploadPhoto.FileName));
18				frmFileUploadPhoto.SaveAs(Path.GetFullPath(frmFileUploadPhoto.FileName));
19				ImageProcess ip = new ImageProcess();
20
21				ip.FileImage = System.Drawing.Image.FromFile(fi.FullName);
22				System.Drawing.Bitmap img = ip.RenderImage();
23
24				MemoryStream stream = new MemoryStream();
25
26				img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
27				int len = (int)stream.Length;
28
29				if (len <= 200000000)
30				{
31
32					byte[] bytes = stream.ToArray(); 
33					bytes = stream.GetBuffer(); 
34
35					aPhoto.Photo = bytes;
36					aPhoto.ApplicationId = ex.ApplicationID;
37					aPhoto.PhotoDate = DateTime.Now;
38					ubo.AddApplicationPhotos(aPhoto);
39				}
40				stream.Close();
41			}
42			else
43			{
44				System.Diagnostics.Debug.Write("Not a valid file");
45			}