423,867 Members | 3,449 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,867 IT Pros & Developers. It's quick & easy.

Upload photo in asp.net content page

P: 25
hi, good evening..
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
  1. <%@ Master Language="C#" AutoEventWireup="true" CodeFile="submaster.master.cs" Inherits="submaster" %>
  2. <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
  3.  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5.  
  6. <html xmlns="http://www.w3.org/1999/xhtml" >
  7. <head id="Head1" runat="server">
  8.     <title>Untitled Page</title>
  9.     <link href="css/styles.css" rel="stylesheet" type="text/css" />
  10.  
  11.       <style type="text/css">
  12.  
  13.         .modalBackground {
  14.             background-color:Gray;
  15.             filter:alpha(opacity=70);
  16.             opacity:0.7;
  17.         }
  18.     </style>  
  19.  
  20. <script language="javascript" type="text/javascript">
  21. // <!CDATA[
  22.  
  23.  
  24.  
  25. // ]]>
  26. </script>
  27. </head>
  28. <body>
  29.     <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//
  30.     <asp:ScriptManager runat="server" ID="scrpt1"/>
  31.     <div>
  32.  
  33.  
  34.  
  35. <!-- HEADER -->
  36.  
  37. <div id="inheader">
  38.  
  39. <div id="logo"><a href="#"></a></div>
  40.     <table>
  41.         <tr>
  42.             <td align="right" style="margin-left: auto; width: 775px; margin-right: auto; height: 21px">
  43.                 <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/logout.png" />
  44.                 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  45.                 &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;</td>
  46.         </tr>
  47.     </table>
  48. <%--<div id="slogan"><img src="http://bytes.com/images/slogan.jpg" alt="" width="292" height="109" /></div>--%>
  49.  
  50. <%--<ul>
  51. <li class="home"><a href="#" title="home page">home page</a></li>
  52. <li class="about"><a href="#" title="about us">about us</a></li>
  53. <li class="services"><a href="#" title="services">services</a></li>
  54. <li class="equipment"><a href="#" title="equipment">equipment</a></li>
  55. <li class="gallery"><a href="#" title="our gallery">our gallery</a></li>
  56. <li class="contact"><a href="#" title="contact us">contact us</a></li>
  57. </ul>--%>
  58.  
  59.  
  60. </div>
  61.  
  62. <table id="menutab" >
  63. <tr>
  64. <td valign="middle" >
  65. <div id="menuhead">
  66. <p>
  67.  
  68.     <span style="font-size: 14pt; color: white">&nbsp;<span
  69.         style="font-size: 13pt"><a href="default.aspx" >Home</a>&nbsp; |&nbsp; <asp:LinkButton runat="server" ID="doctor" Text="Doctor" ForeColor="white" Font-Size="13pt" Width="60px" /> |&nbsp; &nbsp;<asp:LinkButton runat="server" ID="patient" Text="Patients" ForeColor="white" Font-Size="13pt"></asp:LinkButton>&nbsp; &nbsp;|&nbsp; &nbsp;<a href="#">Diagnostic</a>&nbsp; &nbsp;|&nbsp; <a href="#">Pharmacy</a>&nbsp; &nbsp;|&nbsp; <a href="#">
  70.             Hospitals </a>|&nbsp;<asp:LinkButton runat="server" ID="Tools" Text="Tools" ForeColor="White" Font-Size="13pt" OnClick="Tools_Click" ></asp:LinkButton>&nbsp; |&nbsp; <a href="#">Blog</a> | &nbsp;<a href="aboutus.aspx">About Us</a>&nbsp;</span>
  71.  
  72. </span>
  73. </p>
  74. </div>
  75. </td>
  76. </tr>
  77. </table>
  78. <!-- CONTENT -->
  79. <div id="content" style="left: 0px; top: 0px">
  80. <asp:UpdatePanel ID="upd1" runat="server">
  81. <ContentTemplate>
  82.  <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
  83.   </asp:contentplaceholder>
  84.   </ContentTemplate>
  85.   </asp:UpdatePanel>
  86. </div>
  87.  
  88. <!-- FOOTER -->
  89.         <cc1:ModalPopupExtender ID="ModalPopupExtender1" runat="server" CancelControlID="btnCancel"
  90.             OkControlID="btnSignin" TargetControlID="doctor" PopupControlID="Panel1" BackgroundCssClass="modalBackground">
  91.         </cc1:ModalPopupExtender>
  92.         <cc1:ModalPopupExtender ID="ModalPopupExtender2" runat="server" TargetControlID="patient" CancelControlID="btnpCancel" OkControlID="btnpSignin" PopupControlID="Panel2" BackgroundCssClass="modalBackground">
  93.         </cc1:ModalPopupExtender>
  94.         &nbsp;
  95.  
  96. <div id="footer" style="text-align: center; left: 0px; top: 0px;">
  97. <br />
  98.     <span style="font-size: 10pt">
  99. Powered by </span><a href="http://www.meritinfo.com"><span
  100.         style="font-size: 10pt; color:#669933">Merit info Solutions</span></a><span style="font-size: 10pt">
  101.         </span>
  102. </div>
  103.  
  104.     </div>
  105.         <asp:Panel ID="Panel1" runat="server" Height="300px" Width="361px" BackColor="White" BorderColor="DarkGreen" BorderStyle="Ridge">
  106.             <br />
  107.             <br />
  108.             <br />
  109.             &nbsp; &nbsp; <span style="font-size: 12pt; color: dimgray; font-family: Arial">&nbsp;
  110.                 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong><span style="color: #669933">
  111.                     Already A member Log In</span><br />
  112.                 </strong></span>
  113.             <br />
  114.             <table style="width: 251px; height: 88px; margin-left:auto; margin-right:auto " cellspacing="2px" cellpadding="4px">
  115.                 <tr>
  116.                     <td style="width: 100px; height: 20px" align="right">
  117.                         <span style="font-size: 11pt; color: black; font-family: Arial"><strong>User Id</strong></span></td>
  118.                     <td style="width: 102px; height: 20px" colspan="2">
  119.                         <asp:TextBox ID="TextBox1" runat="server" Width="149px"></asp:TextBox></td>
  120.                         <td>
  121.                         </td>
  122.                 </tr>
  123.                 <tr>
  124.                     <td style="width: 100px" align="right">
  125.                         <span style="font-size: 11pt; color: black; font-family: Arial"><strong>Password</strong></span></td>
  126.                     <td style="width: 102px" colspan="2">
  127.                         <asp:TextBox ID="TextBox2" runat="server" TextMode="Password" Width="149px"></asp:TextBox></td>
  128.                         <td>
  129.                         </td>
  130.                 </tr>
  131.                 <tr>
  132.                     <td >
  133.             </td>
  134.                     <td style="width: 82px"  >
  135.             <asp:Button ID="btnSignin" runat="server" Text="Log in" />
  136.                       </td>
  137.                       <td>
  138.             <asp:Button ID="btnCancel" runat="server" Text="Cancel" /></td>
  139.                 </tr>
  140.             </table>
  141.             <br />
  142.             &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
  143.             &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br />
  144.             &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  145.             &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
  146.             <br />
  147.             <br />
  148.             <span style="font-size: 11pt; color: black; font-family: Arial"><strong>&nbsp; &nbsp;
  149.                 &nbsp; New Doctor&nbsp;
  150.             <asp:LinkButton ID="LinkButton1" runat="server" Width="108px" ForeColor="#669933" OnClick="LinkButton1_Click">Sign Up Here</asp:LinkButton></strong></span><br />
  151.             <br />
  152.             <br />
  153.             &nbsp;<br />
  154.             <br />
  155.         </asp:Panel><asp:Panel ID="Panel2" runat="server" Height="300px" Width="361px" BackColor="White" BorderColor="DarkGreen" BorderStyle="Ridge">
  156.             <br />
  157.             <br />
  158.             <br />
  159.             &nbsp; &nbsp; <span style="font-size: 12pt; color: dimgray; font-family: Arial">&nbsp;
  160.                 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong><span style="color: #669933">
  161.                     Already A Member Log In</span><br />
  162.                 </strong></span>
  163.             <br />
  164.             <table style="width: 251px; height: 88px; margin-left:auto; margin-right:auto " cellspacing="2px" cellpadding="4px">
  165.                 <tr>
  166.                     <td style="width: 100px; height: 20px" align="right">
  167.                         <span style="font-size: 11pt; color: black; font-family: Arial"><strong>User Id</strong></span></td>
  168.                     <td style="width: 102px; height: 20px" colspan="2">
  169.                         <asp:TextBox ID="TextBox3" runat="server" Width="149px"></asp:TextBox></td>
  170.                     <td>
  171.                     </td>
  172.                 </tr>
  173.                 <tr>
  174.                     <td style="width: 100px" align="right">
  175.                         <span style="font-size: 11pt; color: black; font-family: Arial"><strong>Password</strong></span></td>
  176.                     <td style="width: 102px" colspan="2">
  177.                         <asp:TextBox ID="TextBox4" runat="server" TextMode="Password" Width="149px"></asp:TextBox></td>
  178.                     <td>
  179.                     </td>
  180.                 </tr>
  181.                 <tr>
  182.                     <td >
  183.                     </td>
  184.                     <td style="width: 82px"  >
  185.                         <asp:Button ID="btnpSignin" runat="server" Text="Log in" />
  186.                     </td>
  187.                     <td>
  188.                         <asp:Button ID="btnpCancel" runat="server" Text="Cancel" /></td>
  189.                 </tr>
  190.             </table>
  191.             <br />
  192.             &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  193.             &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br />
  194.             &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  195.             &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
  196.             <br />
  197.             <br />
  198.             <span style="font-size: 11pt; color: black; font-family: Arial"><strong>&nbsp; &nbsp;
  199.                 &nbsp; New Member&nbsp;
  200.                 <asp:LinkButton ID="LinkButton2" runat="server" ForeColor="#669933" OnClick="LinkButton2_Click"
  201.                     Width="108px">Sign Up Here</asp:LinkButton></strong></span><br />
  202.             <br />
  203.             <br />
  204.             &nbsp;<br />
  205.             <br />
  206.         </asp:Panel>
  207.     </form>
  208. </body>
  209. </html>
  210.  
  211.  

code of Content page:
Expand|Select|Wrap|Line Numbers
  1. <%@ 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"......//
  2.  
  3. <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"  >
  4.  
  5.  
  6. <div id="generalhead" style="width: 297px; height: 15px">
  7.     <span style="font-size: 12pt; color: #669933; font-family: Arial; text-decoration: underline;"><strong>Doctor Sign Up-Additional Information</strong></span>&nbsp;</div>
  8.     <br />
  9.     <br />
  10.         <table style="width: 768px; height: 147px">
  11.         <tr>
  12.         <td style="width: 291px; height: 14px; " valign="top">
  13.         <div id="box">
  14.             <table style="width: 291px; height: 195px" cellspacing="2">
  15.                 <tr>
  16.                     <td style="width: 100px" align="right">
  17.                         Qualification</td>
  18.                     <td style="width: 100px">
  19.                         <asp:TextBox ID="txtqualification" runat="server"></asp:TextBox></td>
  20.                         <td style="width: 9px">
  21.                         <span style="color:Red" >*</span>
  22.                         </td>
  23.                 </tr>
  24.                 <tr>
  25.                     <td style="width: 100px" align="right">
  26.                         Permanent Address</td>
  27.                     <td style="width: 100px" >
  28.                         <asp:TextBox ID="txtpaddress" runat="server" TextMode="MultiLine"></asp:TextBox></td>
  29.                         <td style="width: 9px">
  30.                         <span style="color:Red" >*</span>
  31.                         </td>
  32.                 </tr>
  33.                 <tr>
  34.                     <td style="width: 100px" align="right">
  35.                         Registration No</td>
  36.                     <td style="width: 100px">
  37.                         <asp:TextBox ID="txtregno" runat="server"></asp:TextBox></td>
  38.                         <td style="width: 9px">
  39.                         <span style="color:Red" >*</span>
  40.                         </td>
  41.                 </tr>
  42.                 <tr>
  43.                     <td style="width: 100px" align="right">
  44.                         Experience</td>
  45.                     <td style="width: 100px">
  46.                         <asp:TextBox ID="txtexp" runat="server"></asp:TextBox></td>
  47.                     <td style="width: 9px">
  48.                         <span style="color:Red" >*</span>
  49.                         </td>
  50.                 </tr>
  51.                 <tr>
  52.                     <td style="width: 100px" align="right">
  53.                         Upload Photo</td>
  54.                     <td style="width: 100px">
  55.                         &nbsp;<input id="filUpload" runat="server" type="file" /></td>
  56.                     <td style="width: 9px">
  57.                         <span style="color:Red" >*</span>
  58.                         </td>
  59.                 </tr>
  60.                 <tr>
  61.                     <td align="right" style="width: 100px">
  62.                     </td>
  63.                     <td style="width: 100px">
  64.                         <asp:Button id="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" /></td>
  65.                     <td style="width: 9px">
  66.                     </td>
  67.                 </tr>
  68.             </table>
  69.             </div>
  70.         </td>
  71.         <td style="width: 100px; height: 14px;" valign="top" align="center">
  72.         <div id="box1">
  73.             <table style="width: 291px; height: 195px" cellspacing="2">
  74.                 <tr>
  75.                     <td style="width: 100px" align="right">
  76.                         <span style="font-size: 11pt; color: dimgray; font-family: Arial">
  77.                             <asp:Label id="lblOutput" runat="server"></asp:Label></span></td>
  78.                     <td style="width: 100px">
  79.                         <asp:Image id="imgPicture" runat="server" Height="120px" Width="160px">
  80.                         </asp:Image></td>
  81.                         <td>
  82.                         </td>
  83.                 </tr>
  84.             </table>
  85.             </div>
  86.  
  87.         </td>
  88.         </tr>
  89.         </table>
  90.         <br />
  91.     <table style="width: 765px">
  92.         <tr>
  93.             <td style="width: 100px; height: 14px" align="center">
  94.                 &nbsp;<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/Submit.jpg" OnClick="ImageButton1_Click" /></td>
  95.         </tr>
  96.         <tr>
  97.         <td align="center" style="height: 21px" >
  98.             <span style="font-family: Arial"><span style="font-size: 11pt"><strong>
  99.         (<span style="color:Red">*</span>)</strong></span><span style="color:darkgray"> <span style="font-size: 11pt"><strong>These Fields are Mandatory</strong></span></span>
  100.             </span>
  101.         </td>
  102.         </tr>
  103.     </table>
  104.  
  105. </asp:Content>
  106.  

here is the code of ContentPage.aspx.cs
Expand|Select|Wrap|Line Numbers
  1.  
  2. using System;
  3. using System.Data;
  4. using System.Configuration;
  5. using System.Collections;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Web.UI.HtmlControls;
  12. using System.Data.SqlClient;
  13. using System.Drawing;
  14.  
  15. public partial class Default3 : System.Web.UI.Page
  16. {
  17.     protected void Page_Load(object sender, EventArgs e)
  18.     {
  19.  
  20.     }
  21.     protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
  22.     {
  23.  
  24.     }
  25.  
  26.     protected override void Render(HtmlTextWriter writer)
  27.     {
  28.  
  29.         base.Render(writer);
  30.         this.Page.ClientScript.RegisterForEventValidation(btnUpload.ID);
  31.     }
  32.  
  33.  
  34.     protected  void btnUpload_Click(object sender, System.EventArgs e)
  35. {
  36.  
  37.     // Initialize variables
  38.     string sSavePath;
  39.     string sThumbExtension;
  40.     int intThumbWidth;
  41.     int intThumbHeight;
  42.  
  43.     // Set constant values
  44.     sSavePath = "http://bytes.com/images/";
  45.     sThumbExtension = "_thumb";
  46.     intThumbWidth = 160;
  47.     intThumbHeight = 120;
  48.  
  49.     // If file field isn’t empty
  50.     if (filUpload.PostedFile != null)    //.....Here iam getting filUpload which is HTML input(file) control is empty.....//
  51.     {
  52.         // Check file size (mustn’t be 0)
  53.         HttpPostedFile myFile = filUpload.PostedFile;
  54.         int nFileLen = myFile.ContentLength;
  55.         if (nFileLen == 0)
  56.         {
  57.             lblOutput.Text = "No file was uploaded.";
  58.             return;
  59.         }
  60.  
  61.         // Check file extension (must be JPG)
  62.         if (System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".jpg")
  63.         {
  64.             lblOutput.Text = "The file must have an extension of JPG";
  65.             return;
  66.         }
  67.  
  68.         // Read file into a data stream
  69.         byte[] myData = new Byte[nFileLen];
  70.         myFile.InputStream.Read(myData,0,nFileLen);
  71.  
  72.         // Make sure a duplicate file doesn’t exist.  If it does, keep on appending an 
  73.         // incremental numeric until it is unique
  74.         string sFilename = System.IO.Path.GetFileName(myFile.FileName);
  75.         int file_append = 0;
  76.         while (System.IO.File.Exists(Server.MapPath(sSavePath + sFilename)))
  77.         {
  78.             file_append++;
  79.             sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
  80.                              + file_append.ToString() + ".jpg";
  81.         }
  82.  
  83.         // Save the stream to disk
  84.         System.IO.FileStream newFile
  85.                 = new System.IO.FileStream(Server.MapPath(sSavePath + sFilename), 
  86.                                            System.IO.FileMode.Create);
  87.         newFile.Write(myData,0, myData.Length);
  88.         newFile.Close();
  89.  
  90.         // Check whether the file is really a JPEG by opening it
  91.         System.Drawing.Image.GetThumbnailImageAbort myCallBack = 
  92.                        new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
  93.         Bitmap myBitmap;
  94.         try
  95.         {
  96.             myBitmap = new Bitmap(Server.MapPath(sSavePath + sFilename));
  97.  
  98.             // If jpg file is a jpeg, create a thumbnail filename that is unique.
  99.             file_append = 0;
  100.             string sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
  101.                                                      + sThumbExtension + ".jpg";
  102.             while (System.IO.File.Exists(Server.MapPath(sSavePath + sThumbFile)))
  103.             {
  104.                 file_append++;
  105.                 sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + 
  106.                                file_append.ToString() + sThumbExtension + ".jpg";
  107.             }
  108.  
  109.             // Save thumbnail and output it onto the webpage
  110.             System.Drawing.Image myThumbnail
  111.                     = myBitmap.GetThumbnailImage(intThumbWidth, 
  112.                                                  intThumbHeight, myCallBack, IntPtr.Zero);
  113.             myThumbnail.Save (Server.MapPath(sSavePath + sThumbFile));
  114.             imgPicture.ImageUrl = sSavePath + sThumbFile;
  115.  
  116.             // Displaying success information
  117.             lblOutput.Text = "File uploaded successfully!";
  118.  
  119.             // Destroy objects
  120.             myThumbnail.Dispose();
  121.             myBitmap.Dispose();
  122.         }
  123.         catch (ArgumentException errArgument)
  124.         {
  125.             // The file wasn't a valid jpg file
  126.             lblOutput.Text = "The file wasn't a valid jpg file.";
  127.             System.IO.File.Delete(Server.MapPath(sSavePath + sFilename));
  128.         }
  129.     }
  130. }
  131.  
  132. public bool ThumbnailCallback()
  133. {
  134.     return false;
  135. }        
  136.     }
  137.  
  138.  
  139.  
Dec 24 '08 #1
Share this Question
Share on Google+
3 Replies


P: 25
hi, friends...
here iam working with ContentPage of asp.net...i have to upload image file....
so,iam trying with the below code:....in which i have filUpload(HTML input(file))Control and a webControl button named btnUpload and an image webControl named imgPicture in which iam getting the image in thumbnail form and storing this image and thumbnail in my app folder named images
...all working fine when iam using normal webform/page not ContentPage..i have search the web ..i got answer that i have to add to <@Page of ContentPage EnableEventValidation="true" and add the following code in aspx.cs of ContentPage....
Expand|Select|Wrap|Line Numbers
  1.  
  2. protected override void Render(HtmlTextWriter writer)
  3.     {
  4.  
  5.         base.Render(writer);
  6.         this.Page.ClientScript.RegisterForEventValidation(btnUpload.ID);
  7.     }
  8.  
but still iam not getting any value in filUpload Control ...i.e., (filUpload.PostedFile=null)
Here is my full code of MasterPage,ContentPage.aspx (Default.aspx)and ContentPage.aspx.cs(Default.aspx.cs)

MastePage::
Expand|Select|Wrap|Line Numbers
  1. <%@ Master Language="C#" AutoEventWireup="true" CodeFile="submaster.master.cs" Inherits="submaster" %>
  2. <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
  3.  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5.  
  6. <html xmlns="http://www.w3.org/1999/xhtml" >
  7. <head id="Head1" runat="server">
  8.     <title>Untitled Page</title>
  9.     <link href="css/styles.css" rel="stylesheet" type="text/css" />
  10.  
  11.       <style type="text/css">
  12.  
  13.         .modalBackground {
  14.             background-color:Gray;
  15.             filter:alpha(opacity=70);
  16.             opacity:0.7;
  17.         }
  18.     </style>  
  19.  
  20. <script language="javascript" type="text/javascript">
  21. // <!CDATA[
  22.  
  23.  
  24.  
  25. // ]]>
  26. </script>
  27. </head>
  28. <body>
  29.     <form id="form1" enctype="multipart/form-data" runat="server">
  30.     <asp:ScriptManager runat="server" ID="scrpt1"/>
  31.     <div>
  32.  
  33.  
  34.  
  35. <!-- HEADER -->
  36.  
  37. <div id="inheader">
  38.  
  39. <div id="logo"><a href="#"></a></div>
  40.     <table>
  41.         <tr>
  42.             <td align="right" style="margin-left: auto; width: 775px; margin-right: auto; height: 21px">
  43.                 <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/logout.png" />
  44.                 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  45.                 &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;</td>
  46.         </tr>
  47.     </table>
  48. <%--<div id="slogan"><img src="images/slogan.jpg" alt="" width="292" height="109" /></div>--%>
  49.  
  50. <%--<ul>
  51. <li class="home"><a href="#" title="home page">home page</a></li>
  52. <li class="about"><a href="#" title="about us">about us</a></li>
  53. <li class="services"><a href="#" title="services">services</a></li>
  54. <li class="equipment"><a href="#" title="equipment">equipment</a></li>
  55. <li class="gallery"><a href="#" title="our gallery">our gallery</a></li>
  56. <li class="contact"><a href="#" title="contact us">contact us</a></li>
  57. </ul>--%>
  58.  
  59.  
  60. </div>
  61.  
  62. <table id="menutab" >
  63. <tr>
  64. <td valign="middle" >
  65. <div id="menuhead">
  66. <p>
  67.  
  68.     <span style="font-size: 14pt; color: white">&nbsp;<span
  69.         style="font-size: 13pt"><a href="default.aspx" >Home</a>&nbsp; |&nbsp; <asp:LinkButton runat="server" ID="doctor" Text="Doctor" ForeColor="white" Font-Size="13pt" Width="60px" /> |&nbsp; &nbsp;<asp:LinkButton runat="server" ID="patient" Text="Patients" ForeColor="white" Font-Size="13pt"></asp:LinkButton>&nbsp; &nbsp;|&nbsp; &nbsp;<a href="#">Diagnostic</a>&nbsp; &nbsp;|&nbsp; <a href="#">Pharmacy</a>&nbsp; &nbsp;|&nbsp; <a href="#">
  70.             Hospitals </a>|&nbsp;<asp:LinkButton runat="server" ID="Tools" Text="Tools" ForeColor="White" Font-Size="13pt" OnClick="Tools_Click" ></asp:LinkButton>&nbsp; |&nbsp; <a href="#">Blog</a> | &nbsp;<a href="aboutus.aspx">About Us</a>&nbsp;</span>
  71.  
  72. </span>
  73. </p>
  74. </div>
  75. </td>
  76. </tr>
  77. </table>
  78. <!-- CONTENT -->
  79. <div id="content" style="left: 0px; top: 0px">
  80. <asp:UpdatePanel ID="upd1" runat="server">
  81. <ContentTemplate>
  82.  <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
  83.   </asp:contentplaceholder>
  84.   </ContentTemplate>
  85.   </asp:UpdatePanel>
  86. </div>
  87.  
  88. <!-- FOOTER -->
  89.         <cc1:ModalPopupExtender ID="ModalPopupExtender1" runat="server" CancelControlID="btnCancel"
  90.             OkControlID="btnSignin" TargetControlID="doctor" PopupControlID="Panel1" BackgroundCssClass="modalBackground">
  91.         </cc1:ModalPopupExtender>
  92.         <cc1:ModalPopupExtender ID="ModalPopupExtender2" runat="server" TargetControlID="patient" CancelControlID="btnpCancel" OkControlID="btnpSignin" PopupControlID="Panel2" BackgroundCssClass="modalBackground">
  93.         </cc1:ModalPopupExtender>
  94.         &nbsp;
  95.  
  96. <div id="footer" style="text-align: center; left: 0px; top: 0px;">
  97. <br />
  98.     <span style="font-size: 10pt">
  99. Powered by </span><a href="http://www.meritinfo.com"><span
  100.         style="font-size: 10pt; color:#669933">Merit info Solutions</span></a><span style="font-size: 10pt">
  101.         </span>
  102. </div>
  103.  
  104.     </div>
  105.         <asp:Panel ID="Panel1" runat="server" Height="300px" Width="361px" BackColor="White" BorderColor="DarkGreen" BorderStyle="Ridge">
  106.             <br />
  107.             <br />
  108.             <br />
  109.             &nbsp; &nbsp; <span style="font-size: 12pt; color: dimgray; font-family: Arial">&nbsp;
  110.                 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong><span style="color: #669933">
  111.                     Already A member Log In</span><br />
  112.                 </strong></span>
  113.             <br />
  114.             <table style="width: 251px; height: 88px; margin-left:auto; margin-right:auto " cellspacing="2px" cellpadding="4px">
  115.                 <tr>
  116.                     <td style="width: 100px; height: 20px" align="right">
  117.                         <span style="font-size: 11pt; color: black; font-family: Arial"><strong>User Id</strong></span></td>
  118.                     <td style="width: 102px; height: 20px" colspan="2">
  119.                         <asp:TextBox ID="TextBox1" runat="server" Width="149px"></asp:TextBox></td>
  120.                         <td>
  121.                         </td>
  122.                 </tr>
  123.                 <tr>
  124.                     <td style="width: 100px" align="right">
  125.                         <span style="font-size: 11pt; color: black; font-family: Arial"><strong>Password</strong></span></td>
  126.                     <td style="width: 102px" colspan="2">
  127.                         <asp:TextBox ID="TextBox2" runat="server" TextMode="Password" Width="149px"></asp:TextBox></td>
  128.                         <td>
  129.                         </td>
  130.                 </tr>
  131.                 <tr>
  132.                     <td >
  133.             </td>
  134.                     <td style="width: 82px"  >
  135.             <asp:Button ID="btnSignin" runat="server" Text="Log in" />
  136.                       </td>
  137.                       <td>
  138.             <asp:Button ID="btnCancel" runat="server" Text="Cancel" /></td>
  139.                 </tr>
  140.             </table>
  141.             <br />
  142.             &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
  143.             &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br />
  144.             &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  145.             &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
  146.             <br />
  147.             <br />
  148.             <span style="font-size: 11pt; color: black; font-family: Arial"><strong>&nbsp; &nbsp;
  149.                 &nbsp; New Doctor&nbsp;
  150.             <asp:LinkButton ID="LinkButton1" runat="server" Width="108px" ForeColor="#669933" OnClick="LinkButton1_Click">Sign Up Here</asp:LinkButton></strong></span><br />
  151.             <br />
  152.             <br />
  153.             &nbsp;<br />
  154.             <br />
  155.         </asp:Panel><asp:Panel ID="Panel2" runat="server" Height="300px" Width="361px" BackColor="White" BorderColor="DarkGreen" BorderStyle="Ridge">
  156.             <br />
  157.             <br />
  158.             <br />
  159.             &nbsp; &nbsp; <span style="font-size: 12pt; color: dimgray; font-family: Arial">&nbsp;
  160.                 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong><span style="color: #669933">
  161.                     Already A Member Log In</span><br />
  162.                 </strong></span>
  163.             <br />
  164.             <table style="width: 251px; height: 88px; margin-left:auto; margin-right:auto " cellspacing="2px" cellpadding="4px">
  165.                 <tr>
  166.                     <td style="width: 100px; height: 20px" align="right">
  167.                         <span style="font-size: 11pt; color: black; font-family: Arial"><strong>User Id</strong></span></td>
  168.                     <td style="width: 102px; height: 20px" colspan="2">
  169.                         <asp:TextBox ID="TextBox3" runat="server" Width="149px"></asp:TextBox></td>
  170.                     <td>
  171.                     </td>
  172.                 </tr>
  173.                 <tr>
  174.                     <td style="width: 100px" align="right">
  175.                         <span style="font-size: 11pt; color: black; font-family: Arial"><strong>Password</strong></span></td>
  176.                     <td style="width: 102px" colspan="2">
  177.                         <asp:TextBox ID="TextBox4" runat="server" TextMode="Password" Width="149px"></asp:TextBox></td>
  178.                     <td>
  179.                     </td>
  180.                 </tr>
  181.                 <tr>
  182.                     <td >
  183.                     </td>
  184.                     <td style="width: 82px"  >
  185.                         <asp:Button ID="btnpSignin" runat="server" Text="Log in" />
  186.                     </td>
  187.                     <td>
  188.                         <asp:Button ID="btnpCancel" runat="server" Text="Cancel" /></td>
  189.                 </tr>
  190.             </table>
  191.             <br />
  192.             &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  193.             &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br />
  194.             &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  195.             &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
  196.             <br />
  197.             <br />
  198.             <span style="font-size: 11pt; color: black; font-family: Arial"><strong>&nbsp; &nbsp;
  199.                 &nbsp; New Member&nbsp;
  200.                 <asp:LinkButton ID="LinkButton2" runat="server" ForeColor="#669933" OnClick="LinkButton2_Click"
  201.                     Width="108px">Sign Up Here</asp:LinkButton></strong></span><br />
  202.             <br />
  203.             <br />
  204.             &nbsp;<br />
  205.             <br />
  206.         </asp:Panel>
  207.     </form>
  208. </body>
  209. </html>
  210.  
  211.  

Here is Default.aspx(ContentPage)

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" MasterPageFile="~/submaster.master" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" Title="Untitled Page" EnableEventValidation="true"   %>
  2.  
  3. <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"  >
  4.  
  5.  
  6. <div id="generalhead" style="width: 297px; height: 15px">
  7.     <span style="font-size: 12pt; color: #669933; font-family: Arial; text-decoration: underline;"><strong>Doctor Sign Up-Additional Information</strong></span>&nbsp;</div>
  8.     <br />
  9.     <br />
  10.         <table style="width: 768px; height: 147px">
  11.         <tr>
  12.         <td style="width: 291px; height: 14px; " valign="top">
  13.         <div id="box">
  14.             <table style="width: 291px; height: 195px" cellspacing="2">
  15.                 <tr>
  16.                     <td style="width: 100px" align="right">
  17.                         Qualification</td>
  18.                     <td style="width: 100px">
  19.                         &nbsp;<asp:TextBox ID="txtQualification" runat="server"></asp:TextBox></td>
  20.                         <td style="width: 9px">
  21.                         <span style="color:Red" >*</span>
  22.                         </td>
  23.                 </tr>
  24.                 <tr>
  25.                     <td style="width: 100px" align="right">
  26.                         Permanent Address</td>
  27.                     <td style="width: 100px" >
  28.                         <asp:TextBox ID="txtpaddress" runat="server" TextMode="MultiLine"></asp:TextBox></td>
  29.                         <td style="width: 9px">
  30.                         <span style="color:Red" >*</span>
  31.                         </td>
  32.                 </tr>
  33.                 <tr>
  34.                     <td style="width: 100px" align="right">
  35.                         Registration No</td>
  36.                     <td style="width: 100px">
  37.                         <asp:TextBox ID="txtregno" runat="server"></asp:TextBox></td>
  38.                         <td style="width: 9px">
  39.                         <span style="color:Red" >*</span>
  40.                         </td>
  41.                 </tr>
  42.                 <tr>
  43.                     <td style="width: 100px" align="right">
  44.                         Experience</td>
  45.                     <td style="width: 100px">
  46.                         <asp:TextBox ID="txtexp" runat="server"></asp:TextBox></td>
  47.                     <td style="width: 9px">
  48.                         <span style="color:Red" >*</span>
  49.                         </td>
  50.                 </tr>
  51.                 <tr>
  52.                     <td style="width: 100px" align="right">
  53.                         Upload Photo</td>
  54.                     <td style="width: 100px">
  55.                         &nbsp;<input id="filUpload" runat="server" type="file" /></td>
  56.                     <td style="width: 9px">
  57.                         <span style="color:Red" >*</span>
  58.                         </td>
  59.                 </tr>
  60.                 <tr>
  61.                     <td align="right" style="width: 100px">
  62.                     </td>
  63.                     <td style="width: 100px">
  64.                         <asp:Button id="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" /></td>
  65.                     <td style="width: 9px">
  66.                     </td>
  67.                 </tr>
  68.             </table>
  69.             </div>
  70.         </td>
  71.         <td style="width: 100px; height: 14px;" valign="top" align="center">
  72.         <div id="box1">
  73.             <table style="width: 291px; height: 195px" cellspacing="2">
  74.                 <tr>
  75.                     <td style="width: 100px" align="right">
  76.                         <span style="font-size: 11pt; color: dimgray; font-family: Arial">
  77.                             <asp:Label id="lblOutput" runat="server"></asp:Label></span></td>
  78.                     <td style="width: 100px">
  79.                         <asp:Image id="imgPicture" runat="server" Height="120px" Width="160px">
  80.                         </asp:Image></td>
  81.                         <td>
  82.                         </td>
  83.                 </tr>
  84.             </table>
  85.             </div>
  86.  
  87.         </td>
  88.         </tr>
  89.         </table>
  90.         <br />
  91.     <table style="width: 765px">
  92.         <tr>
  93.             <td style="width: 100px; height: 14px" align="center">
  94.                 &nbsp;<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/Submit.jpg" OnClick="ImageButton1_Click" /></td>
  95.         </tr>
  96.         <tr>
  97.         <td align="center" style="height: 21px" >
  98.             <span style="font-family: Arial"><span style="font-size: 11pt"><strong>
  99.         (<span style="color:Red">*</span>)</strong></span><span style="color:darkgray"> <span style="font-size: 11pt"><strong>These Fields are Mandatory</strong></span></span>
  100.             </span>
  101.         </td>
  102.         </tr>
  103.     </table>
  104.  
  105. </asp:Content>
  106.  
  107.  
  108.  

Code of Default.aspx.cs(ContentPage):

Expand|Select|Wrap|Line Numbers
  1.  
  2. using System;
  3. using System.Data;
  4. using System.Configuration;
  5. using System.Collections;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Web.UI.HtmlControls;
  12. using System.Data.SqlClient;
  13. using System.Drawing;
  14.  
  15. public partial class Default3 : System.Web.UI.Page
  16. {
  17.     protected void Page_Load(object sender, EventArgs e)
  18.     {
  19.  
  20.     }
  21.     protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
  22.     {
  23.  
  24.     }
  25.  
  26.     protected override void Render(HtmlTextWriter writer)
  27.     {
  28.  
  29.         base.Render(writer);
  30.         this.Page.ClientScript.RegisterForEventValidation(btnUpload.ID);
  31.     }
  32.  
  33.  
  34.     protected  void btnUpload_Click(object sender, System.EventArgs e)
  35. {
  36.  
  37.     // Initialize variables
  38.     string sSavePath;
  39.     string sThumbExtension;
  40.     int intThumbWidth;
  41.     int intThumbHeight;
  42.  
  43.     // Set constant values
  44.     sSavePath = "images/";
  45.     sThumbExtension = "_thumb";
  46.     intThumbWidth = 160;
  47.     intThumbHeight = 120;
  48.  
  49.     // If file field isn’t empty
  50.     if (filUpload.PostedFile != null)   .....//here iam not getting value in filUpload Control//.....
  51.     {
  52.         // Check file size (mustn’t be 0)
  53.         HttpPostedFile myFile = filUpload.PostedFile;
  54.         int nFileLen = myFile.ContentLength;
  55.         if (nFileLen == 0)
  56.         {
  57.             lblOutput.Text = "No file was uploaded.";
  58.             return;
  59.         }
  60.  
  61.         // Check file extension (must be JPG)
  62.         if (System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".jpg")
  63.         {
  64.             lblOutput.Text = "The file must have an extension of JPG";
  65.             return;
  66.         }
  67.  
  68.         // Read file into a data stream
  69.         byte[] myData = new Byte[nFileLen];
  70.         myFile.InputStream.Read(myData,0,nFileLen);
  71.  
  72.         // Make sure a duplicate file doesn’t exist.  If it does, keep on appending an 
  73.         // incremental numeric until it is unique
  74.         string sFilename = System.IO.Path.GetFileName(myFile.FileName);
  75.         int file_append = 0;
  76.         while (System.IO.File.Exists(Server.MapPath(sSavePath + sFilename)))
  77.         {
  78.             file_append++;
  79.             sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
  80.                              + file_append.ToString() + ".jpg";
  81.         }
  82.  
  83.         // Save the stream to disk
  84.         System.IO.FileStream newFile
  85.                 = new System.IO.FileStream(Server.MapPath(sSavePath + sFilename), 
  86.                                            System.IO.FileMode.Create);
  87.         newFile.Write(myData,0, myData.Length);
  88.         newFile.Close();
  89.  
  90.         // Check whether the file is really a JPEG by opening it
  91.         System.Drawing.Image.GetThumbnailImageAbort myCallBack = 
  92.                        new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
  93.         Bitmap myBitmap;
  94.         try
  95.         {
  96.             myBitmap = new Bitmap(Server.MapPath(sSavePath + sFilename));
  97.  
  98.             // If jpg file is a jpeg, create a thumbnail filename that is unique.
  99.             file_append = 0;
  100.             string sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
  101.                                                      + sThumbExtension + ".jpg";
  102.             while (System.IO.File.Exists(Server.MapPath(sSavePath + sThumbFile)))
  103.             {
  104.                 file_append++;
  105.                 sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + 
  106.                                file_append.ToString() + sThumbExtension + ".jpg";
  107.             }
  108.  
  109.             // Save thumbnail and output it onto the webpage
  110.             System.Drawing.Image myThumbnail
  111.                     = myBitmap.GetThumbnailImage(intThumbWidth, 
  112.                                                  intThumbHeight, myCallBack, IntPtr.Zero);
  113.             myThumbnail.Save (Server.MapPath(sSavePath + sThumbFile));
  114.             imgPicture.ImageUrl = sSavePath + sThumbFile;
  115.  
  116.             // Displaying success information
  117.             lblOutput.Text = "File uploaded successfully!";
  118.  
  119.             // Destroy objects
  120.             myThumbnail.Dispose();
  121.             myBitmap.Dispose();
  122.         }
  123.         catch (ArgumentException errArgument)
  124.         {
  125.             // The file wasn't a valid jpg file
  126.             lblOutput.Text = "The file wasn't a valid jpg file.";
  127.             System.IO.File.Delete(Server.MapPath(sSavePath + sFilename));
  128.         }
  129.     }
  130. }
  131.  
  132. public bool ThumbnailCallback()
  133. {
  134.     return false;
  135. }        
  136.     }
  137.  
  138.  
  139.  
Dec 25 '08 #2

100+
P: 344
1) Line 44: You don't save the file directly to http:/ some URL instead try this
string onServerPath = Request.PhysicalApplicationPath+@"Folder1\";
ImageFileUpload.SaveAs(onServerPath + ImageFileUpload.FileName);
2)You spent a lot of code and a lot of time to open an IOStream and read/write the file, easier use SaveAs Method

3) "Between only you and me", To get immediate help type only the intended part of code not the whole project, to make others focus on the problem

Thanks
Bassem
Dec 27 '08 #3

Frinavale
Expert Mod 5K+
P: 9,731
Hi Premprakashbhati,

Please do not double post your questions. It makes it difficult for you to get an answer to your question. Also, please only post code relevant to the question you have because it is very hard for us to read through your code to find the few lines that you are talking about. I've merged your threads together since they are the same question.

Anyways, instead of using the HTML upload input you should look into using the .NET FileUpload Control. This control will get around the validation problem you're having and it also offers you tools that let you save the file more easily.

-Frinny
Dec 30 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.