Chris
Chris

Reputation: 3129

When using FileUpload for multiple files, the image names are correct but they are all the same color

I'm uploading images using the FileUploadControl to my database, then to make sure that it worked I print out the images info and then ouput the images to the screen. For some reason that I can't figure out is why the images I am using stay the same color but with different names. I.E

1.png is Red, 2.png is Green, 3.png is blue and 4.png is yellow.

But when I call out the images that were uploaded to the database it now goes as this..

1.png is Red, 2.png is Red, 3.png is Red and 4.png is Red

I have stepped through the code and it seems normal, I have looked at the Markup after it is ran and the image names are all correct. But when I look in the folder it is as I mentioned all the same color as 1.png.

The FileUpload

protected void btnUpload_Click(object sender, EventArgs e)
    {
        //Making sure that images are images
        string[] matchExtension = { ".jpg", ".png", ".gif" };
        string[] matchMimeType = { "image/jpeg", "image/png", "image/gif" };

        if (fpSlideshow.HasFiles)
        {
            string imgName;
            string imgExtension;
            string imgMime;

            foreach (HttpPostedFile upLoadedFile in fpSlideshow.PostedFiles)
            {
                imgName = upLoadedFile.FileName;
                imgExtension = Path.GetExtension(imgName);
                imgMime = fpSlideshow.PostedFile.ContentType;

                if (matchExtension.Contains(imgExtension) && matchMimeType.Contains(imgMime))
                {
                    fpSlideshow.SaveAs(Server.MapPath(@"~/SlideshowImages/" + imgName));
                    string thePath = Server.MapPath(@"~/SlideshowImages/" + imgName).ToString();

                    daccess.AddImagesToSlideshow(imgName, imgExtension, imgMime, thePath);
                }
            }
        }
        daccess.GetImageNames();
        CreateMyStuff();

        for (int i = 0; i != daccess.dsTEST.Tables[0].Rows.Count; i++)
        {
            Response.Write("<br>" + daccess.dsTEST.Tables[0].Rows[i]["ImageName"].ToString() + "<br>" +
                daccess.dsTEST.Tables[0].Rows[i]["ImageType"].ToString() + "<br>" +
                daccess.dsTEST.Tables[0].Rows[i]["ImageMime"].ToString() + "<br>" +
                daccess.dsTEST.Tables[0].Rows[i]["ImageUrl"].ToString() + "<br>");
        }
    }

The CreateMyStuff method

private void CreateMyStuff()
    {

        // Current row count.
        int rowCtr;

        // Total number of cells per row (columns).
        int cellCtr;

        //count number of rows in dataset
        int rN = daccess.dsTEST.Tables[0].Rows.Count;

        for (rowCtr = 0; rowCtr < rN; rowCtr++)
        {
            // Create a new row and add it to the table.
            TableRow tRow = new TableRow();
            Table1.Rows.Add(tRow);

            for (cellCtr = 1; cellCtr <= 2; cellCtr++)
            {
                //
                Button button = new Button();
                //
                HyperLink link = new HyperLink();
                // Create a new cell and add it to the row.
                TableCell tCell = new TableCell();

                if (rowCtr == rN)
                    break;

                string myName = daccess.dsTEST.Tables[0].Rows[rowCtr]["ImageName"].ToString();

                StringBuilder myStrBldr = new StringBuilder();

                myStrBldr.Append("<div class=''>");
                myStrBldr.Append("<img src='SlideshowImages/" + myName + "' />");
                myStrBldr.Append("</div>");


                tCell.Controls.Add(new LiteralControl(myStrBldr.ToString()));

                tRow.Cells.Add(tCell);
                rowCtr++;

                if (cellCtr == 2)
                {
                    rowCtr = rowCtr - 1;
                    break;
                }
            }
        }
    }

Its obvious that its happening in the upload control, but I am not seeing where my mistake is.

Upvotes: 0

Views: 75

Answers (1)

J...
J...

Reputation: 31433

This call :

fpSlideshow.SaveAs(Server.MapPath(@"~/SlideshowImages/" + imgName));

doesn't seem to have any dependence on uploadedFile. It's the same method being called on the same object with a different filename for the saved file each time. If fpSlideshow is an instance of the FileUpload class then I think the default behaviour is to save the first file in PostedFiles. Calling .SaveAs on the uploadedFile should correct the issue.

Upvotes: 2

Related Questions