Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
Hello Friends
i want to upload file in ASP.NET
i am using the following code.


<asp:DataList ID="dlist" runat="server" RepeatColumns="3">
<itemtemplate>
<asp:Image ID="img1" runat="server"
ImageUrl='<%# Eval("Name", "~/Images/{0}") %>'
style="width:200px;height:200px;"/>


<%# Eval("Name", "~/Images/{0}") %>

and on button click

C#
protected void Page_PreRender(object sender, EventArgs e)
   {
       string upFolder = MapPath("~/Images/");
       DirectoryInfo dir = new DirectoryInfo(upFolder);
       dlist.DataSource = dir.GetFiles();
       dlist.DataBind();
   }
   protected void Button1_Click(object sender, EventArgs e)
   {
       try
       {
           if (FileUpload1.HasFile)
           {
               if (CheckFileType(FileUpload1.FileName))
               {
                   String filePath = "~/Images/" + FileUpload1.FileName;
                   FileUpload1.SaveAs(Server.MapPath(filePath));
               }
           }
       }
       catch (Exception ee)
       {
           Response.Write("Message : " + ee.Message);
           Response.Write(ee.StackTrace);
       }
   }
   bool CheckFileType(string fileName)
   {
       string ext = Path.GetExtension(fileName);
       switch (ext.ToLower())
       {
           case ".gif":
               return true;
           case ".png":
               return true;
           case ".jpg":
               return true;
           case ".jpeg":
               return true;
           default:
               return false;
       }
   }


is there any short way to upload the file ...IF yes then please help me..
Please give answer..
Thanks a lot.
Posted
Comments
fjdiewornncalwe 12-Feb-13 13:33pm    
What is the problem. You haven't told us what is going wrong, just what you want to do.

Hi . this code may help you . i don't test them ! ;)
for save file use this :
FileUpload1.SaveAs(Server.MapPath("Books/") +FileUpload1.FileName);


for file-type check use this that return the answer ( seem better that switch!) :
if(
(System.IO.Path.GetExtension(FileUpload1.FileName) == ".jpg") || (System.IO.Path.GetExtension(FileUpload1.FileName) == ".jpeg") || (System.IO.Path.GetExtension(FileUpload1.FileName) == ".bmp") || (System.IO.Path.GetExtension(FileUpload1.FileName) == ".gif") || (System.IO.Path.GetExtension(FileUpload1.FileName) == ".ico") || (System.IO.Path.GetExtension(FileUpload1.FileName) == ".png")
)

{some code}
else
{some code}
 
Share this answer
 
v2
C#
if (FileUpload1.HasFile)
        {
            string name = Path.GetFileName(FileUpload1.PostedFile.FileName);
            string location = Server.MapPath("~/docs/" + name);
            FileUpload1.SaveAs(location);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "insert into upload (FileName) values (@FileName)";
            cmd.Parameters.AddWithValue("@FileName", name);
            cmd.Connection = con;
            con.Open();
            int result = cmd.ExecuteNonQuery();
            if (result > 0)
                lblMessage.Visible = true;
            lblMessage.Text = "File saved";
            cmd.Dispose();
            con.Close();


use custom validation control to control the documents that can be uploaded
SQL
Regular expression to validate file formats for .jpeg or .JPEG or .gif or .GIF or .png or .PNG

Re= /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.jpeg|.JPEG|.gif|.GIF| .png|.PNG)$/;




http://www.aspdotnet-suresh.com/2010/12/how-to-validate-file-type-in-file.html[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900