In this article I will show you how you can upload the file
in asp.net using fileupload control. Here I will show you to upload and image
file to the to the server and display it’s preview in image control.
Some of my previous articles are as follows: Ajax
FileUpload Control In Asp.Net or Multiple FileUpload With Progress Example in
Asp.Net Using C#.Net, Code
to Export GridView to PDF in Asp.Net Using C# and Vb.Net.
So for this article first we will create a new asp.net
application and asp.net application and add a fileupload control and a button
control.
After adding all the controls our page code will look as
shown below:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FileUploadControl.aspx.cs"
Inherits="ProjectDemo_Asp.et.FileUploadControl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>File
Upload with ASP.NET | How to Use FileUpload Control in ASP.Net Using C#.Net</title>
<style type="text/css">
p.MsoNormal
{margin-top:0in;
margin-right:0in;
margin-bottom:10.0pt;
margin-left:0in;
line-height:115%;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1"
runat="server"
Text="Upload
file"
onclick="Button1_Click" />
<br />
<br />
<asp:Label ID="lblmessage" runat="server" Style="color: #FFFFFF; font-weight: 700;
background-color: #FF6699" Text=""></asp:Label>
<p class="MsoNormal" style="text-decoration: underline">
<strong>Preview</strong></p>
<p class="MsoNormal" style="text-decoration: underline">
<asp:Image ID="Image1" runat="server" Height="181px" Width="184px" />
</p>
</form>
</body>
</html>
|
Now generate the click event of the button control and add
the below code
C#.Net
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ProjectDemo_Asp.et
{
public partial class FileUploadControl : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
}
/// <summary>
/// Button click event to upload the selcted file
/// </summary>
/// <param
name="sender"></param>
/// <param
name="e"></param>
protected
void Button1_Click(object
sender, EventArgs e)
{
if
(FileUpload1.HasFile)
{
string
filename = System.IO.Path.GetFileName(FileUpload1.FileName);
FileUpload1.SaveAs(Server.MapPath("~/Folder/")
+ filename);
/*Uploaded
file path*/
string
filePath = "Folder/" + filename;
/*******************************************/
/*Code
to save the file path into data base*/
/*******************************************/
lblmessage.Text = "File uploaded successfully.";
Image1.ImageUrl = filePath;
}
else
{
lblmessage.Text = "Please select file.";
}
}
}
}
|
VB.Net
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace ProjectDemo_Asp.et
Partial Public Class FileUploadControl
Inherits
System.Web.UI.Page
Protected
Sub Page_Load(ByVal
sender As Object,
ByVal e As
EventArgs)
End Sub
'''
<summary>
''' Button
click event to upload the selcted file
'''
</summary>
'''
<param name="sender"></param>
'''
<param name="e"></param>
Protected
Sub Button1_Click(ByVal
sender As Object,
ByVal e As
EventArgs)
If
FileUpload1.HasFile Then
Dim
filename As String
= System.IO.Path.GetFileName(FileUpload1.FileName)
FileUpload1.SaveAs(Server.MapPath("~/Folder/")
+ filename)
'Uploaded
file path
Dim
filePath As String
= "Folder/" + filename
'*****************************************
'Code
to save the file path into data base
'*****************************************
lblmessage.Text = "File uploaded successfully."
Image1.ImageUrl = filePath
Else
lblmessage.Text = "Please select file."
End
If
End Sub
End Class
End Namespace
|
In above code first we have written to get the file name and
after that we have saved the file name in our file directory. After that we can
write the code save the code into data base.
Now we have done run the code to view the output. Now select the file and click on upload button. You will get
the selected file in the code side as shown below.
DOWNLOAD
Fantastic!
ReplyDeleteHas to be the first time I copy and paste code from the internet and works first time!
Nice and easy, very well explained. Well done!
Just a question, seems like there is a limitation on the file size to upload...any ideas how to allow big size files to be uploaded ?
Again, thanks so much for sharing.
Hi thanks for your comment. for uploading large file. u need to do some configration setting. have a look of this link
Deletehttp://blogs.msdn.com/b/prashant_upadhyay/archive/2011/07/13/large-file-upload-issue-in-asp-net.aspx
Access to the path 'e:\HostingSpaces\maro2008\aboazez.com\wwwroot\Folder\111.gif' is denied.
ReplyDeleteplease help me
If you are giving physical path then you will get the error. you need to add a folder in root of the project and while uploading the file server.mappath.
Deleteif this still this solution not work . Please provide the code you are using.
close the door do your work properly
DeleteIf you are giving physical path then you will get the error. you need to add a folder in root of the project and while uploading the file server.mappath.
ReplyDeleteif this still this solution not work . Please provide the code you are using.