Lakshmitha
Lakshmitha

Reputation: 705

FileUpload Control not taking file name

I am using FileUpload Control to upload images, I can select the image using that browse button, but when i try to preview that selected one, i am not getting the file name, its showing empty..

protected void btnImgUpload_Click(object sender, ImageClickEventArgs e)
{
    try
    {
        string strimage;
        string strfilename, strextn;
        if (fupImage.HasFile)
        {

In the above code,fupImage.FileName property should have to selected Image name, but it remains as empty string "" , so fubImage.HasFile condition is going false. I am not getting why the condition is going false, while the file is selected,? what is the problem here?

Thanks in advance

Upvotes: 2

Views: 7451

Answers (2)

Unknown Coder
Unknown Coder

Reputation: 1740

Check for the View-state property of that file uploader Control, If View-state is false, then on post back you'll get empty value

Upvotes: 1

DeveloperX
DeveloperX

Reputation: 4683

what i did to test it is I create an Asp page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>

<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:FileUpload ID="fupImage" runat="server" />
        <asp:Button ID="btnImageUpload" runat="server" onclick="btnImageUpload_Click" 
            Text="Upload" />

    </div>
    </form>
</body>
</html>

and the code behind class is

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

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

        }


        protected void btnImageUpload_Click(object sender, EventArgs e)
        {
            try
            {
                string strimage;
                string strfilename, strextn;
                if (fupImage.HasFile)
                {
                    //do something
                }
            }
            catch
            {
            }
        }
    }
}

and nothing goes wrong with that

I want to say to test it just create a very simple instance and test it in complex environment may be there are some extra rules may prevents normal jobs! and sometimes it looks so unnoraml

Upvotes: 2

Related Questions