Click here to Skip to main content
16,015,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to develop in an application where my user can upload files from page to server. for this purpose i used file upload control in my page. it works perfectly fine. nw i want to apply filter to this control so that user can just upload image files such as .jpg,.gif,.png etc...hw could i do it..??
Posted

Hi,
here is part of my code I use for uploading files into database:

protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
if (txtFile.PostedFile != null && txtFile.PostedFile.ContentLength > 0)
{
string filename = Path.GetFileName(txtFile.PostedFile.FileName);
string extension = Path.GetExtension(filename);
string contentType = txtFile.PostedFile.ContentType;
byte[] document = new byte[txtFile.PostedFile.ContentLength];
int fileId = AddFile(filename, extension, contentType, document);

}
}
}
protected int AddFile(string filename, string extension, string contentType, byte[] document)
{
SqlDatabase sqlDatabase = new SqlDatabase
(ConfigurationManager.ConnectionStrings["dbConnString"].ConnectionString);
string sql = "spFile_Add";
SqlCommand sqlCommand = sqlDatabase.GetStoredProcCommand(sql) as SqlCommand;
Object obj = null;
try
{
sqlDatabase.AddInParameter(sqlCommand, "@Filename", SqlDbType.VarChar, filename);
sqlDatabase.AddInParameter(sqlCommand, "@Extension", SqlDbType.VarChar, extension);
sqlDatabase.AddInParameter(sqlCommand, "@ContentType", SqlDbType.VarChar, contentType);
sqlDatabase.AddInParameter(sqlCommand, "@Document", SqlDbType.VarBinary, document);
obj = sqlDatabase.ExecuteScalar(sqlCommand);
if (obj != null)
return int.Parse(obj.ToString());
else
return 0;
}
catch (Exception err)
{
throw new ApplicationException(err.Message);
}
}

I used Microsoft Enterprise Library

Regards
Robert
 
Share this answer
 
Look here: How to filter files in file upload HTML control[^]. It shows a couple of ways to accomplish this.

ps I don't know if you've ever heard of Google (presumably not) - took about 5 seconds to find that page - try it, you might like it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900