.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ADO.NET >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

File Upload with C#

Posted By:Shashi Ray       Posted Date: September 02, 2008    Points: 10    Category: ADO.NET    URL: http://www.dotnetspark.com  

This code sample will contain the code to upload image in web server. This code uses the File control used to upload image the hosting server.
 

This article will contain the code to upload image in web server. This code uses the File control used to upload image the hosting server.

In the .aspx.cs file on button click event write the below lines of code

Button Click Event;

String strFilename, strMessage,strFoldername;

strFilename=fileUpload.PostedFile.FileName.ToString();

strMessage=uploadFile(strFilename, ConfigurationManager.AppSettings["folderPath"]);

Label1.Text=strMessage;

Label1.ForeColor=System.drawing.Color.Red;

//Method to store image in the server
Public string uploadFile(string fileName, string folderName)
{

if(fileName=="")

{

return "Invalid filename supplied";

}

if(fileUpload.PostedFile.ContentLength==0)

{

return "Invalid file content";

}

fileName=Path.GetFileName(fileName);

if (foldeeName=="")

{

return "Path not found";

}

try

{

if(fileUpload.PostedFile.ContentLength <=2048000)

{

fileUpload.PostedFile.SaveAs((folderName) + "\\" + fileName);

return "file upload successfully";

}

else

{

return "Not Upload file";

}

}

catch (UnauthorizedAccessException ex)

{

return ex.Message + "Permission to upload file deniec";

}

}

}

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend