ninja.ms
07-08-2009, 10:19 PM
I need some help with this image uploader. I am getting an error each time that I try to upload an image through the CMS.
The error that I am receiving is:
C:\InetOxford\images\page\ClientProfileChart.gifThread was being aborted.Upload Failed
I am fairly knew to .net sites and don't know much about image uplaoders that use .aspx
Any help would be great! Thanks!
Here's the code:
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.IO;
using System.Drawing;
using System.Drawing.Imaging;
public partial class userControls_Uploader : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
private string m_AllowType = "image"; //default Images only
public string AllowType
{
get { return m_AllowType; }
set { m_AllowType = value; }
}
private string m_uploadFolder = "/"; //default directory will be the same directory as the page
public string UploadFolder
{
get { return m_uploadFolder; }
set { m_uploadFolder = value; }
}
private int m_MaxFileDimension = 300; //default 300
public int MaxFileDimension
{
get { return m_MaxFileDimension; }
set { m_MaxFileDimension = value; }
}
protected void goForUpload(object sender, EventArgs e)
{
if (MainUploader.HasFile)
{
bool typePicked = false;
DirectoryInfo dir = new DirectoryInfo(Server.MapPath(m_uploadFolder));
//Checking to see which type of files this uploader is ment for
if ((AllowType.ToLower()).Contains("image"))
{
typePicked = true;
//Checking Mime
bool Continue = false;
if (MainUploader.PostedFile.ContentType == "image/pjpeg") { Continue = true; }
if (MainUploader.PostedFile.ContentType == "image/jpeg") { Continue = true; }
if (MainUploader.PostedFile.ContentType == "image/gif") { Continue = true; }
if (Continue)
{
//Checking the Upload Directory
if (dir.Exists)
{
string fileName = MainUploader.PostedFile.FileName.ToString();
string Path = dir.ToString() + "\\" + fileName;
bool moveForward = false;
try
{
MainUploader.PostedFile.SaveAs(Path);
moveForward = true;
}
catch (UnauthorizedAccessException ex)
{
lblOutput.ForeColor = System.Drawing.Color.Red;
lblOutput.Text = ex.Message.ToString() + " Permission to upload file denied";
}
if (moveForward)
{
goForResize(Path);
}
}
else
{
lblOutput.ForeColor = System.Drawing.Color.Red;
lblOutput.Text = "The Upload Directory Doesn't Exists!";
}
}
else
{
lblOutput.ForeColor = System.Drawing.Color.Red;
lblOutput.Text = "The File is not Uploadable Image Type!";
}
}
if ((AllowType.ToLower()).Contains("doc") || (AllowType.ToLower()).Contains("pdf"))
{
typePicked = true;
bool goodFile = false;
if ((AllowType.ToLower()).Contains("pdf"))
{
if (MainUploader.PostedFile.ContentType == "application/pdf" || MainUploader.PostedFile.ContentType == "application/x-pdf")
{
goodFile = true;
string fileName = MainUploader.PostedFile.FileName.ToString();
string Path = dir.ToString() + "\\" + fileName;
try
{
MainUploader.PostedFile.SaveAs(Path);
lblOutput.Text = fileName + " has been uploaded.";
}
catch (UnauthorizedAccessException ex)
{
lblOutput.ForeColor = System.Drawing.Color.Red;
lblOutput.Text = ex.Message.ToString() + " Permission to upload file denied";
}
}
}
if ((AllowType.ToLower()).Contains("doc"))
{
if (MainUploader.PostedFile.ContentType == "application/msword")
{
goodFile = true;
string fileName = MainUploader.PostedFile.FileName.ToString();
string Path = dir.ToString() + "\\" + fileName;
try
{
MainUploader.PostedFile.SaveAs(Path);
lblOutput.Text = fileName + " has been uploaded.";
}
catch (UnauthorizedAccessException ex)
{
lblOutput.ForeColor = System.Drawing.Color.Red;
lblOutput.Text = ex.Message.ToString() + " Permission to upload file denied";
}
}
}
if (goodFile == false)
{
lblOutput.ForeColor = System.Drawing.Color.Red;
lblOutput.Text = "File Not Allowable Upload aborted!";
}
}
if (typePicked == false)
{
lblOutput.ForeColor = System.Drawing.Color.Red;
lblOutput.Text = "AllowType Incorrect!...Please Check Your Spelling.";
}
}
else
{
lblOutput.ForeColor = System.Drawing.Color.Red;
lblOutput.Text = "No File Present!";
}
}
public void goForResize(string Path)
{
FileInfo temp = new FileInfo(Path);
if (temp.Exists)
{
//Create the Resized Image
Bitmap bmp = CreateThumbnail(Path, m_MaxFileDimension, m_MaxFileDimension);
try
{
bmp.Save(Path, ImageFormat.Jpeg);
}
catch (UnauthorizedAccessException ex)
{
lblOutput.ForeColor = System.Drawing.Color.Red;
lblOutput.Text = "File Permissions Error, Access Denied.";
}
}
else
{
lblOutput.ForeColor = System.Drawing.Color.Red;
lblOutput.Text = "File was Uploaded but I forgot where I put it.";
}
}
public static Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight)
{
System.Drawing.Bitmap bmpOut = null;
try
{
Bitmap loBMP = new Bitmap(lcFilename);
ImageFormat loFormat = loBMP.RawFormat;
decimal lnRatio;
int lnNewWidth = 0;
int lnNewHeight = 0;
//*** If the image is smaller than a thumbnail just return it
if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
{
return loBMP;
}
if (loBMP.Width > loBMP.Height)
{
lnRatio = (decimal)lnWidth / loBMP.Width;
lnNewWidth = lnWidth;
decimal lnTemp = loBMP.Height * lnRatio;
lnNewHeight = (int)lnTemp;
}
else
{
lnRatio = (decimal)lnHeight / loBMP.Height;
lnNewHeight = lnHeight;
decimal lnTemp = loBMP.Width * lnRatio;
lnNewWidth = (int)lnTemp;
}
// System.Drawing.Image imgOut =
// loBMP.GetThumbnailImage(lnNewWidth,lnNewHeight,
// null,IntPtr.Zero);
// *** This code creates cleaner (though bigger) thumbnails and properly
// *** and handles GIF files better by generating a white background for
// *** transparent images (as opposed to black)
bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
Graphics g = Graphics.FromImage(bmpOut);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
//g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
loBMP.Dispose();
}
catch
{
return null;
}
return bmpOut;
}
}
The error that I am receiving is:
C:\InetOxford\images\page\ClientProfileChart.gifThread was being aborted.Upload Failed
I am fairly knew to .net sites and don't know much about image uplaoders that use .aspx
Any help would be great! Thanks!
Here's the code:
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.IO;
using System.Drawing;
using System.Drawing.Imaging;
public partial class userControls_Uploader : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
private string m_AllowType = "image"; //default Images only
public string AllowType
{
get { return m_AllowType; }
set { m_AllowType = value; }
}
private string m_uploadFolder = "/"; //default directory will be the same directory as the page
public string UploadFolder
{
get { return m_uploadFolder; }
set { m_uploadFolder = value; }
}
private int m_MaxFileDimension = 300; //default 300
public int MaxFileDimension
{
get { return m_MaxFileDimension; }
set { m_MaxFileDimension = value; }
}
protected void goForUpload(object sender, EventArgs e)
{
if (MainUploader.HasFile)
{
bool typePicked = false;
DirectoryInfo dir = new DirectoryInfo(Server.MapPath(m_uploadFolder));
//Checking to see which type of files this uploader is ment for
if ((AllowType.ToLower()).Contains("image"))
{
typePicked = true;
//Checking Mime
bool Continue = false;
if (MainUploader.PostedFile.ContentType == "image/pjpeg") { Continue = true; }
if (MainUploader.PostedFile.ContentType == "image/jpeg") { Continue = true; }
if (MainUploader.PostedFile.ContentType == "image/gif") { Continue = true; }
if (Continue)
{
//Checking the Upload Directory
if (dir.Exists)
{
string fileName = MainUploader.PostedFile.FileName.ToString();
string Path = dir.ToString() + "\\" + fileName;
bool moveForward = false;
try
{
MainUploader.PostedFile.SaveAs(Path);
moveForward = true;
}
catch (UnauthorizedAccessException ex)
{
lblOutput.ForeColor = System.Drawing.Color.Red;
lblOutput.Text = ex.Message.ToString() + " Permission to upload file denied";
}
if (moveForward)
{
goForResize(Path);
}
}
else
{
lblOutput.ForeColor = System.Drawing.Color.Red;
lblOutput.Text = "The Upload Directory Doesn't Exists!";
}
}
else
{
lblOutput.ForeColor = System.Drawing.Color.Red;
lblOutput.Text = "The File is not Uploadable Image Type!";
}
}
if ((AllowType.ToLower()).Contains("doc") || (AllowType.ToLower()).Contains("pdf"))
{
typePicked = true;
bool goodFile = false;
if ((AllowType.ToLower()).Contains("pdf"))
{
if (MainUploader.PostedFile.ContentType == "application/pdf" || MainUploader.PostedFile.ContentType == "application/x-pdf")
{
goodFile = true;
string fileName = MainUploader.PostedFile.FileName.ToString();
string Path = dir.ToString() + "\\" + fileName;
try
{
MainUploader.PostedFile.SaveAs(Path);
lblOutput.Text = fileName + " has been uploaded.";
}
catch (UnauthorizedAccessException ex)
{
lblOutput.ForeColor = System.Drawing.Color.Red;
lblOutput.Text = ex.Message.ToString() + " Permission to upload file denied";
}
}
}
if ((AllowType.ToLower()).Contains("doc"))
{
if (MainUploader.PostedFile.ContentType == "application/msword")
{
goodFile = true;
string fileName = MainUploader.PostedFile.FileName.ToString();
string Path = dir.ToString() + "\\" + fileName;
try
{
MainUploader.PostedFile.SaveAs(Path);
lblOutput.Text = fileName + " has been uploaded.";
}
catch (UnauthorizedAccessException ex)
{
lblOutput.ForeColor = System.Drawing.Color.Red;
lblOutput.Text = ex.Message.ToString() + " Permission to upload file denied";
}
}
}
if (goodFile == false)
{
lblOutput.ForeColor = System.Drawing.Color.Red;
lblOutput.Text = "File Not Allowable Upload aborted!";
}
}
if (typePicked == false)
{
lblOutput.ForeColor = System.Drawing.Color.Red;
lblOutput.Text = "AllowType Incorrect!...Please Check Your Spelling.";
}
}
else
{
lblOutput.ForeColor = System.Drawing.Color.Red;
lblOutput.Text = "No File Present!";
}
}
public void goForResize(string Path)
{
FileInfo temp = new FileInfo(Path);
if (temp.Exists)
{
//Create the Resized Image
Bitmap bmp = CreateThumbnail(Path, m_MaxFileDimension, m_MaxFileDimension);
try
{
bmp.Save(Path, ImageFormat.Jpeg);
}
catch (UnauthorizedAccessException ex)
{
lblOutput.ForeColor = System.Drawing.Color.Red;
lblOutput.Text = "File Permissions Error, Access Denied.";
}
}
else
{
lblOutput.ForeColor = System.Drawing.Color.Red;
lblOutput.Text = "File was Uploaded but I forgot where I put it.";
}
}
public static Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight)
{
System.Drawing.Bitmap bmpOut = null;
try
{
Bitmap loBMP = new Bitmap(lcFilename);
ImageFormat loFormat = loBMP.RawFormat;
decimal lnRatio;
int lnNewWidth = 0;
int lnNewHeight = 0;
//*** If the image is smaller than a thumbnail just return it
if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
{
return loBMP;
}
if (loBMP.Width > loBMP.Height)
{
lnRatio = (decimal)lnWidth / loBMP.Width;
lnNewWidth = lnWidth;
decimal lnTemp = loBMP.Height * lnRatio;
lnNewHeight = (int)lnTemp;
}
else
{
lnRatio = (decimal)lnHeight / loBMP.Height;
lnNewHeight = lnHeight;
decimal lnTemp = loBMP.Width * lnRatio;
lnNewWidth = (int)lnTemp;
}
// System.Drawing.Image imgOut =
// loBMP.GetThumbnailImage(lnNewWidth,lnNewHeight,
// null,IntPtr.Zero);
// *** This code creates cleaner (though bigger) thumbnails and properly
// *** and handles GIF files better by generating a white background for
// *** transparent images (as opposed to black)
bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
Graphics g = Graphics.FromImage(bmpOut);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
//g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
loBMP.Dispose();
}
catch
{
return null;
}
return bmpOut;
}
}