Upload only image file
We can define the file type to upload . Here the code will allow
upload only image file. If you try to upload other type file then it will print
one message.
protected void Button1_Click(object sender, EventArgs
e)
    {
        if
(FileUpload1.HasFile)
        {
            if
((FileUpload1.PostedFile.ContentType).Equals("image/pjpeg"))
            {
                try
                {
                    FileUpload1.SaveAs("D:\\" + FileUpload1.FileName);
                    Response.Write("Filename-" + FileUpload1.FileName + "<br/>");
                    Response.Write("File size-" +
FileUpload1.PostedFile.ContentLength + "<br/>");
                    Response.Write("File Type-" +
FileUpload1.PostedFile.ContentType + "<br/>");
                }
                catch
(Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
            else
            {
                Response.Write("Choose image .jpg file to upload");
            }
        }
   }
N.B If you want to restrict large file from upload then use the
following code 
if (fileUpload.PostedFile.ContentLength<=2048000)
  
         
{     
                                     fileUpload.PostedFile.SaveAs(Server.MapPath(folderName)+"\\"+fileName);    
                  
         
          return
"File uploaded successfully";     
 
          
}
           
else
  {
      
return "Unable to upload,file exceeds
maximum limit";
           
}
No comments:
Post a Comment