Wednesday, March 21, 2012

Problem in file uploading with Ajax

I want to upload an image on server. My source below is working well. But when I keep the same things in Update Panel it didn't work. Can some one help me on this strange behavior please?

Thanks!

ImageUpload.aspx.cs
 
using System;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
 
namespace Articles
{
   publicclass Default : Page
   {
       public HtmlInputFile   Picture;
       public Label           Message;
       public Image           UploadedPic;
 
       publicvoid UploadImage(object sender, EventArgs e)
       {
           if (Picture.PostedFile !=null)
           {
               string PicType = Picture.PostedFile.ContentType.ToString();
               if (PicType == "image/jpeg" || PicType == "image/gif" || PicType == "image/pjpeg"
                   || PicType == "image/bmp")
               {
                   try
                   {
                       string PicName = Picture.PostedFile.FileName;
                       string NewPicName = (string)Session.SessionID + "_" + PicName.Substring(PicName.LastIndexOf(@dotnet.itags.org."\")+1);
                       string UploadPath = Request.PhysicalApplicationPath + @dotnet.itags.org."\Upload\";
 
                       Picture.PostedFile.SaveAs(UploadPath + NewPicName);
 
                       Message.Text = "Picture successfully uploaded to server!";
                       UploadedPic.AlternateText = "Uploaded picture...";
                       UploadedPic.ImageUrl = UploadPath + NewPicName;
                       UploadedPic.Visible =true;
                   }
                   catch (Exception exc)
                   {
                       Message.Text = exc.ToString();
                   }
               }
               else
                   Message.Text = "File format not supported!";
           }
       }
   }
}

ImageUpload.aspx
 
<%@dotnet.itags.org. Page Language="C#" Codebehind="ImageUpload1.aspx.cs" Inherits="Articles.Default" %>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>Image Upload #1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body>
 
<h3>Image Upload #1</h3>
<form enctype="multipart/form-data" runat="server">
   <table>
       <tr>
           <td>Image:</td>
           <td><input id="Picture" type="file" runat="server"/></td>
       </tr>
   </table>
   <br/>
   <input type="submit" value="Upload >>>" onserverclick="UploadImage" runat="server"/>
</form>
 
<asp:label id="Message" runat="server"/>
 
<hr>
 
<asp:image id="UploadedPic" visible="False" runat="server"/>
 
</body>
</html>

Hi,

unfortunately the FileUpload control, like some other controls, don't work together with the UpdatePanel. So no go.

You can check the controls out that don't work with it here:

Taken from the documentation:http://ajax.asp.net/docs/overview/UpdatePanelOverview.aspx

Controls that Are Not Compatible with UpdatePanel Controls

The following ASP.NET controls are not compatible with partial-page updates, and are therefore not supported inside anUpdatePanel control:

TreeView andMenu controls.

No comments:

Post a Comment