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 » Code Snippets » C# Syntax »

File Upload using C#


Posted Date: 17 Aug 2009    Resource Type: Code Snippets    Category: C# Syntax
Author: Prakash AlamandaMember Level: Gold    
Rating: 1 out of 5Points: 8



Description :


This code shows how to upload a file using C#

HTML Code

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" /><br />
<br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
</div>
</form>
</body>
</html>


Code Behind in c#

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;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
try
{
FileUpload1.SaveAs("C:\\" + FileUpload1.FileName);
Label1.Text = "File Stored in the path <b>" + FileUpload1.PostedFile.FileName + "</b>";
}
catch (Exception exc)
{
Label1.Text = "Error in uploading the file. Details: " + exc.Message;
}
}
else
{
Label1.Text = "Select the file before you upload";
}
}
}


Attachments






Responses

Author: pankaj gupta    22 Aug 2009Member Level: Silver   Points : 1
I am getting this error when i am trying to use this in local host. the error is
Parser Error Message: Unknown server tag 'asp:ScriptManager'.


Please tell me what to do...


Author: Prakash Alamanda    22 Aug 2009Member Level: Gold   Points : 1
actually remove that <asp:scriptmanager> tag since it was a AJAX enabled website i used Scriptmanager. u need not use it. just remove that ttag


Author: pankaj gupta    22 Aug 2009Member Level: Silver   Points : 1
As you have told that remove this line
I have removed and try to run but it is again showing a error:
BC30456: 'Button1_Click' is not a member of 'ASP.default_aspx'.

What to do now?
Thank You.


Author: Prakash Alamanda    22 Aug 2009Member Level: Gold   Points : 1
You better delete the Onclick="" code and in the deisgn mode double click your button and paste the logic inside the eent. hope it works.



Author: pankaj gupta    22 Aug 2009Member Level: Silver   Points : 1
Sir,now it is working now.but it is not showing the message that your file has been uploaded. In your attachments,it has been mentioned that it will show.


Author: Prakash Alamanda    22 Aug 2009Member Level: Gold   Points : 1
Do u have the Label control as i kept. if so it will work. just check with t namespaces and controls onceagain if not, just attach ur code file il check

Regards


Author: pankaj gupta    22 Aug 2009Member Level: Silver   Points : 1
actually sir , i am new . I am just following your code and trying to understand. I have attached the code . Please check it. Thank you continuously replying me.


WebSite2.rar
Author: Prakash Alamanda    22 Aug 2009Member Level: Gold   Points : 1
Friend

In the Default.aspx design page.... in the top give the following code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

I didnt see the above code in your application


Author: Prakash Alamanda    22 Aug 2009Member Level: Gold   Points : 0
If you still have prob, kindly check with the attached project


Projects.rar
Author: pankaj gupta    22 Aug 2009Member Level: Silver   Points : 1
Thank you sir . Now it is working. i am found my error. Actually as i was new so i was deleting the default codes coming when i was creating a website project and replacing it with your code.That's why it was not giving the output. Thank you so much for this long and continuous interaction.





Author: pankaj gupta    22 Aug 2009Member Level: Silver   Points : 1
But sir,this is showing the path of the file it is not uploading the files. But your heading is File Uploading using C#. Why it is so sir?



Author: Prakash Alamanda    22 Aug 2009Member Level: Gold   Points : 1
yeah it will ofcourse upload. You check my previous post. i' ve attached the project. just run it to see the file upload. call me prakash my dear friend

regards


Author: Prakash Alamanda    22 Aug 2009Member Level: Gold   Points : 1
I won be available from now as im leavin. il reply for ur any further queries by monday

Bye
Regards
Prakash Alamanda


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
File Upload using c#  .  

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: Simple Abstract Class
Previous Resource: Get Cell Tower info
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use