codewarrior
codewarrior

Reputation: 193

FileUpload.HasFile always shows false

Iam loading three fileupload controls dynamically thru the following code.

protected void Page_Load(object sender, EventArgs e)
{          
       Table tblDocuments = new Table();
        tblDocuments.CellPadding=8;
        tblDocuments.CellSpacing=8;

        TableRow tr7 = new TableRow();

        TableCell tc13 = new TableCell();
        Label lblSRV=new Label();
        lblSRV.Text="SRV:";
        tc13.BackColor = System.Drawing.ColorTranslator.FromHtml("#4FB0B0");
        lblSRV.ForeColor=System.Drawing.Color.Black;
        lblSRV.Font.Bold=true;
        tc13.Controls.Add(lblSRV);
        tr7.Cells.Add(tc13);

        TableCell tc14 = new TableCell();
        fuUploadSRV.ID = "SRV";
        tc14.Controls.Add(fuUploadSRV);
        tc14.HorizontalAlign = HorizontalAlign.Center;
        tc14.VerticalAlign = VerticalAlign.Middle;

        tr7.Cells.Add(tc14);
        tblDocuments.Rows.Add(tr7);

        TableRow tr8 = new TableRow();

        TableCell tc15 = new TableCell();           
        Label lblMAtrialCertificate = new Label();
        lblMAtrialCertificate.Text = "Material/Product Certificate:";
        lblMAtrialCertificate.ForeColor = System.Drawing.Color.Black;
        lblMAtrialCertificate.Font.Bold = true;
        tc15.BackColor = System.Drawing.ColorTranslator.FromHtml("#4FB0B0");
        tc15.Controls.Add(lblMAtrialCertificate);
        tr8.Cells.Add(tc15);

        TableCell tc16 = new TableCell();
        fuUploadMaterialCertificate.ID = "MaterialCertificate";
        tc16.Controls.Add(fuUploadMaterialCertificate);
        tc16.HorizontalAlign = HorizontalAlign.Center;
        tc16.VerticalAlign = VerticalAlign.Middle;

        tr8.Cells.Add(tc16);
        tblDocuments.Rows.Add(tr8);


        TableRow tr9 = new TableRow();

        TableCell tc17 = new TableCell();
        Label lblPurchaseOrder = new Label();
        lblPurchaseOrder.Text = "Purchase Order:";
        lblPurchaseOrder.ForeColor = System.Drawing.Color.Black;
        lblPurchaseOrder.Font.Bold = true;
        tc17.BackColor = System.Drawing.ColorTranslator.FromHtml("#4FB0B0");
        tc17.Controls.Add(lblPurchaseOrder);
        tr9.Cells.Add(tc17);

        TableCell tc18 = new TableCell();
        fuUploadPurchaseOrder.ID="Purchase";
        tc18.Controls.Add(fuUploadPurchaseOrder);
        tc18.HorizontalAlign = HorizontalAlign.Center;
        tc18.VerticalAlign = VerticalAlign.Middle;

        tr9.Cells.Add(tc18);
        tblDocuments.Rows.Add(tr9);

        placeSubmit.Controls.Add(tblDocuments);

}

Iam processing the file in the button click event

void btnSubmit_Click(object sender, EventArgs e)
{

   if (fuUploadSRV.HasFile)
        {
            string srvFilename = Path.GetFileName(fuUploadSRV.PostedFile.FileName);
            Stream fs = fuUploadSRV.PostedFile.InputStream;
            int filesize = fuUploadSRV.PostedFile.ContentLength;
            BinaryReader br = new BinaryReader(fs);
            byte[] content = br.ReadBytes(filesize);
            Objects.UpdateSRVFileDetails(srvFilename, content,txtSRV.Text.ToString().Trim());

        }

        if (fuUploadMaterialCertificate.HasFile)
        {
            string materialFilename = Path.GetFileName(fuUploadMaterialCertificate.PostedFile.FileName);
            Stream fs = fuUploadMaterialCertificate.PostedFile.InputStream;
            int filesize = fuUploadMaterialCertificate.PostedFile.ContentLength;
            BinaryReader br = new BinaryReader(fs);
            byte[] content = br.ReadBytes(filesize);
            Objects.UpdateMaterialCertificateFileDetails(materialFilename, content, txtSRV.Text.ToString().Trim());
        }

        if (fuUploadPurchaseOrder.HasFile)
        {
            string poFilename = Path.GetFileName(fuUploadPurchaseOrder.PostedFile.FileName);
            Stream fs = fuUploadPurchaseOrder.PostedFile.InputStream;
            int filesize = fuUploadPurchaseOrder.PostedFile.ContentLength;
            BinaryReader br = new BinaryReader(fs);
            byte[] content = br.ReadBytes(filesize);
            Objects.UpdatePurchaseOrderFileDetails(poFilename, content, txtPurchaseOrderNo.Text.ToString().Trim());
        }

}

Now while debugging, though I am choosing a file, the fuUploadSRV.HasFile,fuUploadMaterialCertificate.HasFile,fuUploadPurchaseOrder.HasFile is always returning false.

Am I missing something?

Upvotes: 1

Views: 2805

Answers (2)

codewarrior
codewarrior

Reputation: 193

The file upload control is in update panel. When I removed the update panel, the control worked as predicted. But I dint understand how update panel played a role, but removing this has solved my problem.

Upvotes: 0

Shan Plourde
Shan Plourde

Reputation: 8706

Did you try updating your <form runat="server" to include enctype="multipart/form-data"?

Weird things may happen without it!

Upvotes: 1

Related Questions