Check a file image or not,C#

Posted by Raja_89 under C# on 4/17/2013 | Points: 10 | Views : 6542 | Status : [Member] | Replies : 4
Hai Techies
How can i check an uploaded file is image or not using File upload control in asp.net Web Application

Regards




Responses

Posted by: harikrishna149-10896 on: 4/18/2013 [Member] Starter | Points: 25

Up
0
Down
Hi check below code..

Default.aspx
---------------------------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<input type="file" id="myFile" name="myFile" />
<asp:Button runat="server" ID="btnUpload" OnClick="btnUploadClick" Text="Upload" /><br />
<asp:Label ID="LblMessage" runat="server"></asp:Label>
</form>
</body>
</html>






Default.aspx.cs
-------------------------------

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnUploadClick(object sender, EventArgs e)
{
HttpPostedFile file = Request.Files["myFile"];
string fname = Path.GetFileName(file.FileName);
if (file.ContentType == "image/jpeg" || file.ContentType == "image/png" || file.ContentType == "image/gif")
{
file.SaveAs(Server.MapPath(Path.Combine("~/Images/", fname)));
LblMessage.Text = fname + " Image is Uploaded successfully";
}
else
LblMessage.Text = "ImageFile is not valid only jpeg,png,gif files are allowed ";

}
}

Thanks & Regards
Hari Krishna

Raja_89, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Appanrajmca on: 5/3/2013 [Member] Starter | Points: 25

Up
0
Down
It may help to u !!!! refer this!!!
 Download source file

Raja_89, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Appanrajmca on: 5/3/2013 [Member] Starter | Points: 25

Up
0
Down
i attached for windows application!!!only

Raja_89, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Learningtorise on: 5/3/2013 [Member] Starter | Points: 25

Up
0
Down
If Jquery Can be used then use this code inside document.ready function:


// $('#FILEUPLOADID"').change(function () {

// var val = $(this).val();

// switch (val.substring(val.lastIndexOf('.') + 1).toLowerCase()) {
// case 'gif': case 'bmp': case 'tiff': case 'jpeg': case 'jpg': case 'png':
// break;
// default:
// $(this).val('');
// // error message here
// alert("Please select only image files.");
// break;
// }
// });

Just uncomment the code :)

http://hashtagakash.wordpress.com/

Raja_89, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response