FileUpload and update panel

M

Mukesh

i have coded like this


<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>

<asp:Label ID="Label1" runat="server">
<asp:FileUpload ID="fupLogo" runat="server" Visible="False">

<asp:Button ID="Button2" runat="server" Text="Upload"
OnClick="Button2_Click" Visible="False" />

</asp:FileUpload><asp:Button ID="Button1" runat="server"
OnClick="Button1_Click" Text="show Uploader " />
</ContentTemplate>
<Triggers>
<asp:postBackTrigger ControlID="Button2">
</asp:postBackTrigger>
</Triggers>
</asp:UpdatePanel>


code behind>>>>>>>>>>>>>>>>>>>

protected void Button2_Click(object sender, EventArgs e)
{
if (fupLogo.HasFile)
{
string fileFormat = fupLogo.PostedFile.ContentType;
Label1.Text = fupLogo.PostedFile.ContentType;
if (string.Compare(fileFormat, "image/jpeg", true) == 0 ||
string.Compare(fileFormat, "image/png", true) == 0 ||
string.Compare(fileFormat, "image/gif", true) == 0)
{
Label1.Text += "file format supported<br/>";
}
else
{
Label1.Text +="file format not supported<br/>";
}
}
else
{
Label1.Text += "file not exist<br/>";
}
}

protected void Button1_Click(object sender, EventArgs e)
{
fupLogo.Visible = true;
Button2.Visible=true;
}



this behaves like on page load it shows a button then the onclick of the
button it shows a fileupload and upload button


Expected: when i cliks upload button after selecting any file
ouput must be "file format supported/not suported"

actual: but the output is "file not exist".
 
W

Walter Wang [MSFT]

Hi Mukesh,

Please note that the FileUpload control will not work in an UpdatePanel
during an async postback. This is because of security reasons, the async
postback is executed on the client-side. Think what will happen if it would
be possible for client-side script to access the file system. It's
documented here:

http://ajax.asp.net/docs/overview/UpdatePanelOverview.aspx

(see section "Controls that Are Not Compatible with UpdatePanel Controls")


Also refer to Eilon Lipton's blog for more information:

#Why don't file uploads work during async postbacks? - Eilon Lipton's Blog
http://weblogs.asp.net/leftslipper/archive/2007/03/30/why-don-t-file-uploads
-work-during-async-postbacks.aspx


Hope this helps.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

marss

i have coded like this

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>

<asp:Label ID="Label1" runat="server">
<asp:FileUpload ID="fupLogo" runat="server" Visible="False">

<asp:Button ID="Button2" runat="server" Text="Upload"
OnClick="Button2_Click" Visible="False" />

</asp:FileUpload><asp:Button ID="Button1" runat="server"
OnClick="Button1_Click" Text="show Uploader " />
</ContentTemplate>
<Triggers>
<asp:postBackTrigger ControlID="Button2">
</asp:postBackTrigger>
</Triggers>
</asp:UpdatePanel>

code behind>>>>>>>>>>>>>>>>>>>

protected void Button2_Click(object sender, EventArgs e)
{
if (fupLogo.HasFile)
{
string fileFormat = fupLogo.PostedFile.ContentType;
Label1.Text = fupLogo.PostedFile.ContentType;
if (string.Compare(fileFormat, "image/jpeg", true) == 0 ||
string.Compare(fileFormat, "image/png", true) == 0 ||
string.Compare(fileFormat, "image/gif", true) == 0)
{
Label1.Text += "file format supported<br/>";
}
else
{
Label1.Text +="file format not supported<br/>";
}
}
else
{
Label1.Text += "file not exist<br/>";
}

}

protected void Button1_Click(object sender, EventArgs e)
{
fupLogo.Visible = true;
Button2.Visible=true;
}

this behaves like on page load it shows a button then the onclick of the
button it shows a fileupload and upload button

Expected: when i cliks upload button after selecting any file
ouput must be "file format supported/not suported"

actual: but the output is "file not exist".

Use style="display:none" instead of Visible="False". In this case
PostBackTrigger for Button2 works correctly.
Example:
..aspx file
<asp:FileUpload ID="fupLogo" runat="server" style="display:none"></
asp:FileUpload>
<asp:Button ID="Button2" runat="server" Text="Upload"
OnClick="Button2_Click" style="display:none" />

..cs file
protected void Button1_Click(object sender, EventArgs e)
{
fupLogo.Style.Remove("display");
Button2.Style.Remove("display");
}
Regards,
Mykola
 
M

marss

<asp:FileUpload ID="fupLogo" runat="server" Visible="False">

<asp:Button ID="Button2" runat="server" Text="Upload"
OnClick="Button2_Click" Visible="False" />

Use style="display:none" instead of Visible="False". In this case
PostBackTrigger for Button2 works correctly.
Example:
..aspx file
<asp:FileUpload ID="fupLogo" runat="server" style="display:none"></
asp:FileUpload>
<asp:Button ID="Button2" runat="server" Text="Upload"
OnClick="Button2_Click" style="display:none" />

..cs file
protected void Button1_Click(object sender, EventArgs e)
{
fupLogo.Style.Remove("display");
Button2.Style.Remove("display");
}
Regards,
Mykola
 
W

Walter Wang [MSFT]

Hi Mukesh,

I was replying too quick and didn't noticed the Button2 is causing a normal
postback instead of an async postback, therefore my previous reply doesn't
apply for your question. Sorry about that.

Thanks Mykola for the correct fix, I've tested this and it's working
correctly. Please test this on your side and let us know the result.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Mukesh

Walter said:
Hi Mukesh,

I was replying too quick and didn't noticed the Button2 is causing a normal
postback instead of an async postback, therefore my previous reply doesn't
apply for your question. Sorry about that.

Thanks Mykola for the correct fix, I've tested this and it's working
correctly. Please test this on your side and let us know the result.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.


Dear
Walter and mykola
Thanks for the reply
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top