Urgent! Why is this not working?!
I have a file upload class, and in it's constructor it needs to be able to take in an array of possible file extensions (using the new params keyword)
I won't know how many or what extensions are being passed in, but once they are only those passed in are allowed to be uploaded. The class I'm having problems with inherits from it's parent class that works fine and doesn't restrict anything.
The new inherited class compiles fine, but still allows anything to uploaded, even when I've specified ".doc" as the only type allowed.
Here's how one would create and use it:
UploadFileOptions myUpload = new UploadFileOptions("C:\testfiles\", FileUpload1, ".doc", ".txt") //etc... any number of extensions could be inserted here.
Like I said, it compiles fine, but using the example above it should only allow .doc and .txt files to be inserted.
Here is the code for the inherited class and below it is the code for it's base class
--
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.Collections.Generic;
using System.Collections;
using System.Text;
using System.IO;
using TACC;
namespace TACC
{
public class UploadFileOptions : UploadFile
{
private Dictionary<string, string> allowedTypes = new Dictionary<string, string>();
public UploadFileOptions(string filePath, FileUpload fileUpload, params string[] fileTypes)
: base(filePath, fileUpload)
{
foreach (string f in fileTypes)
{
allowedTypes.Add(f, null);
}
}
public void Upload(string type)
{
try
{
if (!allowedTypes.ContainsKey(Path.GetExtension(type)))
throw new Exception("That file type is not allowed!");
else
{
_fileUpload.SaveAs(_filePath);
_status = "Received: " + _fileUpload.FileName + " Content Type: " +
_fileUpload.PostedFile.ContentType + " Length: " + _fileUpload.PostedFile.ContentLength;
}
}
catch (System.IO.IOException e)
{
_status = e.Message.ToString();
}
catch (System.Exception e)
{
_status = e.Message.ToString();
}
}//end upload( )
}
}
BASE CLASS CODE
-
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;
namespace TACC
{
public class UploadFile
{
protected string _filePath;
protected FileUpload _fileUpload;
protected string _status;
public UploadFile(string filePath, FileUpload fileUpload)
{
this._filePath = filePath;
this._fileUpload = fileUpload;
}
public string Status
{
get
{
return _status;
}
set
{
_status = value;
}
}
public virtual void Upload()
{
_filePath += _fileUpload.FileName;
if (_fileUpload.HasFile)
{
try
{
_fileUpload.SaveAs(_filePath);
_status = "Received: " + _fileUpload.FileName + " Content Type: " +
_fileUpload.PostedFile.ContentType + " Length: " + _fileUpload.PostedFile.ContentLength;
}
catch (System.IO.IOException e)
{
_status = e.Message.ToString();
}
catch (System.Exception e)
{
_status = e.Message.ToString();
}
}//end if
else
{
_status = "No file was uploaded";
}
}
}
}
PLEASE SOMEONE HELP!

