C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

File uploading in asp.net using C# and diplaying in the grid


Posted Date: 12 May 2006    Resource Type: Articles    Category: .NET Framework
Author: Brahmam NallapatiMember Level: Bronze    
Rating: 1 out of 5Points: 7



Introduction


design basically consists of a text area for entering the description abt the file and html input = file ,(fileupload control) in the description field we need to enter the description about the file and in the html input control we need browse for a file which u want to upload

Paragraph Heading 1


 

private void cmdSubmit_ServerClick(object sender, EventArgs e)
{
if(this.File1.PostedFile != null)
{
HttpPostedFile filePosted = this.File1.PostedFile;
string tempfileName = filePosted.FileName;
if(tempfileName == "")
{
//Response.Write("");
return;
}
tempfileName = tempfileName.Substring(tempfileName.LastIndexOf("\\")+1);
int version = CheckForFileNameInDb(tempfileName);
if(version > 0 && version.ToString() != null)
version += 1;
else
version = 1;
int pos = tempfileName.IndexOf(".");
string temp = "[" + version + "]";
string fileName = tempfileName.Insert(pos,temp);
string localPath = System.Configuration.ConfigurationSettings.AppSettings["articleFolder"].ToString();
string fullPath = localPath + "/" + fileName;
string upLoadedBy = this.cmbUploadedBy.SelectedItem.Text;
string fileDesc = this.txtDesc.Value;
filePosted.SaveAs(Server.MapPath(fullPath));
try
{
if(lconn.State == ConnectionState.Open)
lconn.Close();
lconn.Open();
string cmdText = String.Format("Insert into dms_doc_details(dms_docname,dms_docdesc,dms_docuploadedby,dms_docversion) values('{0}','{1}','{2}','{3}')",tempfileName,fileDesc,upLoadedBy,version);
cmd = new MySqlCommand(cmdText,lconn); cmd.ExecuteNonQuery();
//Response.Write("");
//Response.Write("");
// Response.Redirect("ListDocuments.aspx");
}
catch(MySqlException ex)
{
Response.Write(ex.Message);
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
finally
{
lconn.Close();
cmd.Dispose();
}
}
ClearControls();





Paragraph Heading N


Using the above code the file will be uploaded to the specified path that has been mentioned in the web.config and description , filenames will be stored in the database , if the user was uploading the same documnet the version of the document will be increased by one

< ItemTemplate >
< a > href="FileData.aspx?did=<%# DataBinder.Eval(Container.DataItem,"dms_docid")%>"><%# DataBinder.Eval(Container.DataItem,"dms_docname")>%> </a>
< /ItemTemplate >
above code is the asp code for providing the hyperlink to the document name in the datagrid when we click on the link the document has to be opened or saved.

code for opening the document

string path = MapPath(fullPath);
string fileName = Path.GetFileName(path);
string extension = Path.GetExtension(path);
string type = "";
if(extension != "")
{
switch(extension.ToLower())
{
case ".htm":
case ".html":
type = "text/HTML";
break;

case ".txt":
type = "text/plain";
break;

case ".doc":
case ".rtf":
type = "Application/msword";
break;
}
}
int pos = fileName.IndexOf(".")- fileName.IndexOf("[");
fileName = fileName.Remove(fileName.IndexOf("["),pos);
if(forcedownload)
Response.AppendHeader("content-disposition","attachment; filename=" + fileName);
if(type != "")
Response.ContentType = type;
Response.WriteFile(path);
Response.End();

Summary






Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Parameters in C#
Previous Resource: Programming Serial Ports Using Visual Basic 2005
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use