i am going to upload an image in a web form .....for that iam using HTML input(file) control and one web control button i.e., Upload_Button()
here is the code ...its work fine when iam using a normal web page... but can't in content page....
Code in Master Page
Expand|Select|Wrap|Line Numbers
- <%@ Master Language="C#" AutoEventWireup="true" CodeFile="submaster.master.cs" Inherits="submaster" %>
- <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head id="Head1" runat="server">
- <title>Untitled Page</title>
- <link href="css/styles.css" rel="stylesheet" type="text/css" />
- <style type="text/css">
- .modalBackground {
- background-color:Gray;
- filter:alpha(opacity=70);
- opacity:0.7;
- }
- </style>
- <script language="javascript" type="text/javascript">
- // <!CDATA[
- // ]]>
- </script>
- </head>
- <body>
- <form id="form1" enctype="multipart/form-data" runat="server"> //i have added here enctype="mutipart/form-data" ... its works fine when iam writing it in normal webpage .aspx form//
- <asp:ScriptManager runat="server" ID="scrpt1"/>
- <div>
- <!-- HEADER -->
- <div id="inheader">
- <div id="logo"><a href="#"></a></div>
- <table>
- <tr>
- <td align="right" style="margin-left: auto; width: 775px; margin-right: auto; height: 21px">
- <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/logout.png" />
-
- </td>
- </tr>
- </table>
- <%--<div id="slogan"><img src="http://bytes.com/images/slogan.jpg" alt="" width="292" height="109" /></div>--%>
- <%--<ul>
- <li class="home"><a href="#" title="home page">home page</a></li>
- <li class="about"><a href="#" title="about us">about us</a></li>
- <li class="services"><a href="#" title="services">services</a></li>
- <li class="equipment"><a href="#" title="equipment">equipment</a></li>
- <li class="gallery"><a href="#" title="our gallery">our gallery</a></li>
- <li class="contact"><a href="#" title="contact us">contact us</a></li>
- </ul>--%>
- </div>
- <table id="menutab" >
- <tr>
- <td valign="middle" >
- <div id="menuhead">
- <p>
- <span style="font-size: 14pt; color: white"> <span
- style="font-size: 13pt"><a href="default.aspx" >Home</a> | <asp:LinkButton runat="server" ID="doctor" Text="Doctor" ForeColor="white" Font-Size="13pt" Width="60px" /> | <asp:LinkButton runat="server" ID="patient" Text="Patients" ForeColor="white" Font-Size="13pt"></asp:LinkButton> | <a href="#">Diagnostic</a> | <a href="#">Pharmacy</a> | <a href="#">
- Hospitals </a>| <asp:LinkButton runat="server" ID="Tools" Text="Tools" ForeColor="White" Font-Size="13pt" OnClick="Tools_Click" ></asp:LinkButton> | <a href="#">Blog</a> | <a href="aboutus.aspx">About Us</a> </span>
- </span>
- </p>
- </div>
- </td>
- </tr>
- </table>
- <!-- CONTENT -->
- <div id="content" style="left: 0px; top: 0px">
- <asp:UpdatePanel ID="upd1" runat="server">
- <ContentTemplate>
- <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
- </asp:contentplaceholder>
- </ContentTemplate>
- </asp:UpdatePanel>
- </div>
- <!-- FOOTER -->
- <cc1:ModalPopupExtender ID="ModalPopupExtender1" runat="server" CancelControlID="btnCancel"
- OkControlID="btnSignin" TargetControlID="doctor" PopupControlID="Panel1" BackgroundCssClass="modalBackground">
- </cc1:ModalPopupExtender>
- <cc1:ModalPopupExtender ID="ModalPopupExtender2" runat="server" TargetControlID="patient" CancelControlID="btnpCancel" OkControlID="btnpSignin" PopupControlID="Panel2" BackgroundCssClass="modalBackground">
- </cc1:ModalPopupExtender>
-
- <div id="footer" style="text-align: center; left: 0px; top: 0px;">
- <br />
- <span style="font-size: 10pt">
- Powered by </span><a href="http://www.meritinfo.com"><span
- style="font-size: 10pt; color:#669933">Merit info Solutions</span></a><span style="font-size: 10pt">
- </span>
- </div>
- </div>
- <asp:Panel ID="Panel1" runat="server" Height="300px" Width="361px" BackColor="White" BorderColor="DarkGreen" BorderStyle="Ridge">
- <br />
- <br />
- <br />
- <span style="font-size: 12pt; color: dimgray; font-family: Arial">
- <strong><span style="color: #669933">
- Already A member Log In</span><br />
- </strong></span>
- <br />
- <table style="width: 251px; height: 88px; margin-left:auto; margin-right:auto " cellspacing="2px" cellpadding="4px">
- <tr>
- <td style="width: 100px; height: 20px" align="right">
- <span style="font-size: 11pt; color: black; font-family: Arial"><strong>User Id</strong></span></td>
- <td style="width: 102px; height: 20px" colspan="2">
- <asp:TextBox ID="TextBox1" runat="server" Width="149px"></asp:TextBox></td>
- <td>
- </td>
- </tr>
- <tr>
- <td style="width: 100px" align="right">
- <span style="font-size: 11pt; color: black; font-family: Arial"><strong>Password</strong></span></td>
- <td style="width: 102px" colspan="2">
- <asp:TextBox ID="TextBox2" runat="server" TextMode="Password" Width="149px"></asp:TextBox></td>
- <td>
- </td>
- </tr>
- <tr>
- <td >
- </td>
- <td style="width: 82px" >
- <asp:Button ID="btnSignin" runat="server" Text="Log in" />
- </td>
- <td>
- <asp:Button ID="btnCancel" runat="server" Text="Cancel" /></td>
- </tr>
- </table>
- <br />
-
- <br />
-
-
- <br />
- <br />
- <span style="font-size: 11pt; color: black; font-family: Arial"><strong>
- New Doctor
- <asp:LinkButton ID="LinkButton1" runat="server" Width="108px" ForeColor="#669933" OnClick="LinkButton1_Click">Sign Up Here</asp:LinkButton></strong></span><br />
- <br />
- <br />
- <br />
- <br />
- </asp:Panel><asp:Panel ID="Panel2" runat="server" Height="300px" Width="361px" BackColor="White" BorderColor="DarkGreen" BorderStyle="Ridge">
- <br />
- <br />
- <br />
- <span style="font-size: 12pt; color: dimgray; font-family: Arial">
- <strong><span style="color: #669933">
- Already A Member Log In</span><br />
- </strong></span>
- <br />
- <table style="width: 251px; height: 88px; margin-left:auto; margin-right:auto " cellspacing="2px" cellpadding="4px">
- <tr>
- <td style="width: 100px; height: 20px" align="right">
- <span style="font-size: 11pt; color: black; font-family: Arial"><strong>User Id</strong></span></td>
- <td style="width: 102px; height: 20px" colspan="2">
- <asp:TextBox ID="TextBox3" runat="server" Width="149px"></asp:TextBox></td>
- <td>
- </td>
- </tr>
- <tr>
- <td style="width: 100px" align="right">
- <span style="font-size: 11pt; color: black; font-family: Arial"><strong>Password</strong></span></td>
- <td style="width: 102px" colspan="2">
- <asp:TextBox ID="TextBox4" runat="server" TextMode="Password" Width="149px"></asp:TextBox></td>
- <td>
- </td>
- </tr>
- <tr>
- <td >
- </td>
- <td style="width: 82px" >
- <asp:Button ID="btnpSignin" runat="server" Text="Log in" />
- </td>
- <td>
- <asp:Button ID="btnpCancel" runat="server" Text="Cancel" /></td>
- </tr>
- </table>
- <br />
-
- <br />
-
-
- <br />
- <br />
- <span style="font-size: 11pt; color: black; font-family: Arial"><strong>
- New Member
- <asp:LinkButton ID="LinkButton2" runat="server" ForeColor="#669933" OnClick="LinkButton2_Click"
- Width="108px">Sign Up Here</asp:LinkButton></strong></span><br />
- <br />
- <br />
- <br />
- <br />
- </asp:Panel>
- </form>
- </body>
- </html>
code of Content page:
Expand|Select|Wrap|Line Numbers
- <%@ Page Language="C#" MasterPageFile="~/submaster.master" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" Title="Untitled Page" EnableEventValidation="true" %> //......Here i have add EnableEventValidation="true"......//
- <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server" >
- <div id="generalhead" style="width: 297px; height: 15px">
- <span style="font-size: 12pt; color: #669933; font-family: Arial; text-decoration: underline;"><strong>Doctor Sign Up-Additional Information</strong></span> </div>
- <br />
- <br />
- <table style="width: 768px; height: 147px">
- <tr>
- <td style="width: 291px; height: 14px; " valign="top">
- <div id="box">
- <table style="width: 291px; height: 195px" cellspacing="2">
- <tr>
- <td style="width: 100px" align="right">
- Qualification</td>
- <td style="width: 100px">
- <asp:TextBox ID="txtqualification" runat="server"></asp:TextBox></td>
- <td style="width: 9px">
- <span style="color:Red" >*</span>
- </td>
- </tr>
- <tr>
- <td style="width: 100px" align="right">
- Permanent Address</td>
- <td style="width: 100px" >
- <asp:TextBox ID="txtpaddress" runat="server" TextMode="MultiLine"></asp:TextBox></td>
- <td style="width: 9px">
- <span style="color:Red" >*</span>
- </td>
- </tr>
- <tr>
- <td style="width: 100px" align="right">
- Registration No</td>
- <td style="width: 100px">
- <asp:TextBox ID="txtregno" runat="server"></asp:TextBox></td>
- <td style="width: 9px">
- <span style="color:Red" >*</span>
- </td>
- </tr>
- <tr>
- <td style="width: 100px" align="right">
- Experience</td>
- <td style="width: 100px">
- <asp:TextBox ID="txtexp" runat="server"></asp:TextBox></td>
- <td style="width: 9px">
- <span style="color:Red" >*</span>
- </td>
- </tr>
- <tr>
- <td style="width: 100px" align="right">
- Upload Photo</td>
- <td style="width: 100px">
- <input id="filUpload" runat="server" type="file" /></td>
- <td style="width: 9px">
- <span style="color:Red" >*</span>
- </td>
- </tr>
- <tr>
- <td align="right" style="width: 100px">
- </td>
- <td style="width: 100px">
- <asp:Button id="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" /></td>
- <td style="width: 9px">
- </td>
- </tr>
- </table>
- </div>
- </td>
- <td style="width: 100px; height: 14px;" valign="top" align="center">
- <div id="box1">
- <table style="width: 291px; height: 195px" cellspacing="2">
- <tr>
- <td style="width: 100px" align="right">
- <span style="font-size: 11pt; color: dimgray; font-family: Arial">
- <asp:Label id="lblOutput" runat="server"></asp:Label></span></td>
- <td style="width: 100px">
- <asp:Image id="imgPicture" runat="server" Height="120px" Width="160px">
- </asp:Image></td>
- <td>
- </td>
- </tr>
- </table>
- </div>
- </td>
- </tr>
- </table>
- <br />
- <table style="width: 765px">
- <tr>
- <td style="width: 100px; height: 14px" align="center">
- <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/Submit.jpg" OnClick="ImageButton1_Click" /></td>
- </tr>
- <tr>
- <td align="center" style="height: 21px" >
- <span style="font-family: Arial"><span style="font-size: 11pt"><strong>
- (<span style="color:Red">*</span>)</strong></span><span style="color:darkgray"> <span style="font-size: 11pt"><strong>These Fields are Mandatory</strong></span></span>
- </span>
- </td>
- </tr>
- </table>
- </asp:Content>
here is the code of ContentPage.aspx.cs
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Data.SqlClient;
- using System.Drawing;
- public partial class Default3 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
- {
- }
- protected override void Render(HtmlTextWriter writer)
- {
- base.Render(writer);
- this.Page.ClientScript.RegisterForEventValidation(btnUpload.ID);
- }
- protected void btnUpload_Click(object sender, System.EventArgs e)
- {
- // Initialize variables
- string sSavePath;
- string sThumbExtension;
- int intThumbWidth;
- int intThumbHeight;
- // Set constant values
- sSavePath = "http://bytes.com/images/";
- sThumbExtension = "_thumb";
- intThumbWidth = 160;
- intThumbHeight = 120;
- // If file field isn’t empty
- if (filUpload.PostedFile != null) //.....Here iam getting filUpload which is HTML input(file) control is empty.....//
- {
- // Check file size (mustn’t be 0)
- HttpPostedFile myFile = filUpload.PostedFile;
- int nFileLen = myFile.ContentLength;
- if (nFileLen == 0)
- {
- lblOutput.Text = "No file was uploaded.";
- return;
- }
- // Check file extension (must be JPG)
- if (System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".jpg")
- {
- lblOutput.Text = "The file must have an extension of JPG";
- return;
- }
- // Read file into a data stream
- byte[] myData = new Byte[nFileLen];
- myFile.InputStream.Read(myData,0,nFileLen);
- // Make sure a duplicate file doesn’t exist. If it does, keep on appending an
- // incremental numeric until it is unique
- string sFilename = System.IO.Path.GetFileName(myFile.FileName);
- int file_append = 0;
- while (System.IO.File.Exists(Server.MapPath(sSavePath + sFilename)))
- {
- file_append++;
- sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
- + file_append.ToString() + ".jpg";
- }
- // Save the stream to disk
- System.IO.FileStream newFile
- = new System.IO.FileStream(Server.MapPath(sSavePath + sFilename),
- System.IO.FileMode.Create);
- newFile.Write(myData,0, myData.Length);
- newFile.Close();
- // Check whether the file is really a JPEG by opening it
- System.Drawing.Image.GetThumbnailImageAbort myCallBack =
- new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
- Bitmap myBitmap;
- try
- {
- myBitmap = new Bitmap(Server.MapPath(sSavePath + sFilename));
- // If jpg file is a jpeg, create a thumbnail filename that is unique.
- file_append = 0;
- string sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
- + sThumbExtension + ".jpg";
- while (System.IO.File.Exists(Server.MapPath(sSavePath + sThumbFile)))
- {
- file_append++;
- sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) +
- file_append.ToString() + sThumbExtension + ".jpg";
- }
- // Save thumbnail and output it onto the webpage
- System.Drawing.Image myThumbnail
- = myBitmap.GetThumbnailImage(intThumbWidth,
- intThumbHeight, myCallBack, IntPtr.Zero);
- myThumbnail.Save (Server.MapPath(sSavePath + sThumbFile));
- imgPicture.ImageUrl = sSavePath + sThumbFile;
- // Displaying success information
- lblOutput.Text = "File uploaded successfully!";
- // Destroy objects
- myThumbnail.Dispose();
- myBitmap.Dispose();
- }
- catch (ArgumentException errArgument)
- {
- // The file wasn't a valid jpg file
- lblOutput.Text = "The file wasn't a valid jpg file.";
- System.IO.File.Delete(Server.MapPath(sSavePath + sFilename));
- }
- }
- }
- public bool ThumbnailCallback()
- {
- return false;
- }
- }