File upload
how to upload file in asp.net web application?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Aug 27, 2013, 11:08 PM EDT
for that create a table in sql server.
id int(set identity)
filename varchar(50)
filetype varchar(50)
filesize int
in front end insert these values to database table.
to get file extension use below code:
FileInfo fi = new FileInfo(filename);
string ext = fi.Extension;
to get file size use below code:
int fileSize = FileUploadControl.PostedFile.ContentLength;
now save these data in database.
sample:
protected void UploadButton_Click(object sender, EventArgs e)
{
if (FileUploadControl.HasFile)
{
try
{
string filename = Path.GetFileName(FileUploadControl.FileName);
int fileSize = FileUploadControl.PostedFile.ContentLength;
FileInfo fi = new FileInfo(filename);
string ext = fi.Extension;
FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
SqlConnection con = new SqlConnection("your connection string");
con.Open();
SqlCommand cmd = new SqlCommand("insert into FilesTable(filename,filetype,filesize) values(@Name,@type,@size)", con);
cmd.Parameters.AddWithValue("@Name", filename);
cmd.Parameters.AddWithValue("@type", ext);
cmd.Parameters.AddWithValue("@size", fileSize);
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 27, 2013, 9:03 PM EDT
Please refer the below link
http://www.codeproject.com/Articles/1757/File-Upload-with-ASP-NET
Regards,
Iftikar
Nitesh KejriwalRank #253 · 9.9K reputationPosted Aug 27, 2013, 9:04 PM EDT
http://www.c-sharpcorner.com/UploadFile/mcbkruse/how-to-create-a-file-upload-in-Asp-Net/
http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/file-upload-in-Asp-Net-mvc-application/
http://www.c-sharpcorner.com/uploadfile/rahul4_saxena/uploading-multiple-files-in-Asp-Net-2-0-part-ii/
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Aug 27, 2013, 9:06 PM EDT
refer to this link
http://www.aspdotnet-suresh.com/2012/02/saveupload-files-in-folder-and-download.html
http://asp.net-tutorials.com/controls/file-upload-control/
Rosi Sreenivasa Reddy137 reputationPosted Aug 27, 2013, 10:15 PM EDT
i am using below link we send to me.
coding working properly but i have to ask one thing that in output i am clicking Browse file added after that if i click on the upload button it is showing No File Selected.
i have to know that is use of upload
please send urgently..............
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Aug 27, 2013, 10:19 PM EDT
pz share your aspx and .cs code.
Rosi Sreenivasa Reddy137 reputationPosted Aug 27, 2013, 10:23 PM EDT
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>
aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void UploadButton_Click(object sender, EventArgs e)
{
if (FileUploadControl.HasFile)
{
try
{
string filename = Path.GetFileName(FileUploadControl.FileName);
FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
}
}
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Aug 27, 2013, 10:31 PM EDT
checked your code its working..
after upload it will clear the fileupload control.
if you want to see the file. select your solution and right click on that and refesh folder .
plz provide more details what problem you are getting in that code.
Rosi Sreenivasa Reddy137 reputationPosted Aug 27, 2013, 10:48 PM EDT
it is showing properly what u said...............
Rosi Sreenivasa Reddy137 reputationPosted Aug 27, 2013, 10:52 PM EDT
i want to save details of file which i upload(filename,filetype,filesize) in database.
in sql server 2008 r2
Bineesh ViswanathRank #1,681 · 907 reputationPosted Aug 27, 2013, 11:01 PM EDT
if (FileUploadControl.HasFile)
{
int length = FileUploadControl.PostedFile.ContentLength;
byte[] imgbyte = new byte[length];
HttpPostedFile img = FileUploadControl.PostedFile;
img.InputStream.Read(imgbyte, 0, length);
string EmplyeeName = txtEmployeeName.Text;
if (sqlCon.State == ConnectionState.Closed)
{
sqlCon.Open();
}
SqlCommand c = new SqlCommand("PhotoAdd", sqlCon);
c.CommandType = CommandType.StoredProcedure;
c.Parameters.Add("@employeeName", SqlDbType.VarChar).Value = EmplyeeName;
c.Parameters.Add("@Photo", SqlDbType.Image).Value = imgbyte;
int Incount = c.ExecuteNonQuery();
if (Incount > 0)
{
HttpContext.Current.Response.Write("");
}
}
GalleryView();
dlImages.DataBind();
Rosi Sreenivasa Reddy137 reputationPosted Aug 28, 2013, 12:17 AM EDT
for explaining this topic