File Upload with restricted extensions 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!

[3749 byte] By [Tryston02] at [2007-12-16]
# 1
Check to see what Path.GetExtension(type) returns. Also, you probably want to make your dictionary contain all upper case or all lower case. Then you would convert the results of Path.GetExtension(type) to the same case before doing the comparison.

Upon further review, the signature of the base class' upload method is different from the derived class. It's possible your client is not passing the string and therefore is calling the base class implementation.

dkocur2 at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 2
Ok, I made the changes you suggested. It's partly working. It throws an exception like it is supposed to if you try to upload a file with an extension you didn't specify in the constructor, however, when you try to upload an allowed file it throws an error: Here is the error:
Could not find a part of the path 'C:\FileTest\'
Here is the calling code:
-
UploadFileOptions myUpload;
private string _path = @"C:\FileTest\";

protected void Page_Init(object sender, EventArgs e)
{
myUpload = new UploadFileOptions(_path, FileUpload1, ".doc");
}

When I use it's unrestricting base class, it works, something in the constructor is not working right. Here's the complete code again with the new changes made you suggested.
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 override void Upload()
{
string fileExtension = Path.GetExtension(_fileUpload.FileName);
try
{
if (!allowedTypes.ContainsKey(fileExtension))
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

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";
}
}

}
}

Tryston02 at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 3
in the overriden Upload method you are not appending the name of the File to the _filePath variable. Due to this when you call on Save there is no filename to be saved, just the path hence the error is raised. YOu need to add the line

_filePath += _fileUpload.FileName;

in the Upload method to make the code work

Regards,
Saurabh Nandu
www.MasterCSharp.com
www.AksTech.com

SaurabhNandu at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...
# 4
You did it!!!! It worked!!! THANK YOU!!! I've been pulling my hair out over this!!
THANK YOU!!!
Take care,
Chris
Tryston02 at 2007-9-9 > top of Msdn Tech,Visual C#,Visual C# General...