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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sanjeeb LenkaPosted Aug 28, 2013, 3:08 AM
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);
}
}
Rosi Sreenivasa ReddyPosted Aug 28, 2013, 4:17 AM
for explaining this topic
Bineesh ViswanathPosted Aug 28, 2013, 3:01 AM
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 ReddyPosted Aug 28, 2013, 2:52 AM
i want to save details of file which i upload(filename,filetype,filesize) in database.
in sql server 2008 r2
Rosi Sreenivasa ReddyPosted Aug 28, 2013, 2:48 AM
it is showing properly what u said...............
Sanjeeb LenkaPosted Aug 28, 2013, 2:31 AM
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 ReddyPosted Aug 28, 2013, 2:23 AM
<%@ 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 LenkaPosted Aug 28, 2013, 2:19 AM
pz share your aspx and .cs code.
Rosi Sreenivasa ReddyPosted Aug 28, 2013, 2:15 AM
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 LenkaPosted Aug 28, 2013, 1:06 AM
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/
Nitesh KejriwalPosted Aug 28, 2013, 1:04 AM
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/
Iftikar HussainPosted Aug 28, 2013, 1:03 AM
Please refer the below link
http://www.codeproject.com/Articles/1757/File-Upload-with-ASP-NET
Regards,
Iftikar