Nida
Nida

Reputation: 1702

All uploaded files are not displayed in the links

I have written as

 protected void btnUpload_Click(object sender, EventArgs e)
    {
        HttpFileCollection fileCollection = Request.Files;
        for (int i = 0; i < fileCollection.Count; i++)
        {
            HttpPostedFile uploadfile = fileCollection[i];
            string fileName = Path.GetFileName(uploadfile.FileName);
            if (uploadfile.ContentLength > 0)
            {
                uploadfile.SaveAs(Server.MapPath("~/UploadFiles/") + fileName);
                 lblMessage.Text += fileName + "Saved Successfully<br>";
                 hyperlnk.Text = fileName.ToString() + "Saved Successfully<br>";
               // hyperlnk.Attributes.Add("href", Server.MapPath("/UploadFiles/") + fileName);
                 hyperlnk.NavigateUrl="~/UploadFiles/" + fileName;
                //lblMessage.Text= "<a href=" + "/UploadFiles/" + fileName +">"+fileName+"</a>";
            }
        }
    }

When we Upload multiple files, only one of them is displayed in link as shown in screenshot enter image description here

Please help me

Upvotes: 0

Views: 57

Answers (3)

spike.y
spike.y

Reputation: 399

I think you need to create 1 hyperlink at each iteration, instead off assigning a different value to that same hyperlink on each iteration.

E.g.:

for (int i = 0; i < fileCollection.Count; i++)
        {
             HyperLink link = new HyperLink();
             link.Text = "whatever";

             form1.Controls.Add(link);
        }

I would expand on this example by adding necessary code for displaying the link but I am not well-versed in Web Forms.

Upvotes: 1

Victor
Victor

Reputation: 628

You need to add panel of type Panel on the page instead of hyperlnk and use it for a container for your links the code will look like following:

panel.Controls.Clear();
for (int i = 0; i < fileCollection.Count; i++)
{
    HttpPostedFile uploadfile = fileCollection[i];
    string fileName = Path.GetFileName(uploadfile.FileName);
    if (uploadfile.ContentLength > 0)
    {
        uploadfile.SaveAs(Server.MapPath("~/UploadFiles/") + fileName);
        lblMessage.Text += fileName + "Saved Successfully<br>";
        HyperLink dynamHyperLink = new HyperLink();
        dynamHyperLink.Text = fileName.ToString() + "Saved Successfully<br>";
        // hyperlnk.Attributes.Add("href", Server.MapPath("/UploadFiles/") + fileName);
        dynamHyperLink.NavigateUrl = "~/UploadFiles/" + fileName;

        panel.Controls.Add(dynamHyperLink);
        //lblMessage.Text= "<a href=" + "/UploadFiles/" + fileName +">"+fileName+"</a>";
    }
}

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1502835

You appear to have a single hyperlink object, accessed via a variable called hyperlnk.

If you want to have multiple links, you'll need to create multiple links.

(You can see all the label text, because you're appending to the label text on each iteration of the loop, whereas you're replacing the hyperlink text/url. You can't just append to the hyperlink, because you want to have several independent links.)

Upvotes: 1

Related Questions