Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对多个文件使用FileUpload时,图像名称正确,但颜色相同_C#_Asp.net_File Upload_Webforms - Fatal编程技术网

C# 对多个文件使用FileUpload时,图像名称正确,但颜色相同

C# 对多个文件使用FileUpload时,图像名称正确,但颜色相同,c#,asp.net,file-upload,webforms,C#,Asp.net,File Upload,Webforms,我正在使用FileUploadControl将图像上载到我的数据库,然后为了确保它有效,我打印了图像信息,然后将图像输出到屏幕上。 出于某种原因,我不明白为什么我使用的图像保持相同的颜色,但名称不同。即 1.png为红色,2.png为绿色,3.png为蓝色,4.png为黄色 但当我调出上传到数据库的图像时,它现在是这样的 1.png为红色,2.png为红色,3.png为红色,4.png为红色 我已经仔细阅读了代码,看起来很正常,我在运行完标记后查看了标记,并且图像名称都是正确的。但是当我查看文件

我正在使用FileUploadControl将图像上载到我的数据库,然后为了确保它有效,我打印了图像信息,然后将图像输出到屏幕上。 出于某种原因,我不明白为什么我使用的图像保持相同的颜色,但名称不同。即

1.png为红色,2.png为绿色,3.png为蓝色,4.png为黄色

但当我调出上传到数据库的图像时,它现在是这样的

1.png为红色,2.png为红色,3.png为红色,4.png为红色

我已经仔细阅读了代码,看起来很正常,我在运行完标记后查看了标记,并且图像名称都是正确的。但是当我查看文件夹时,它就像我提到的一样,颜色与1.png相同

文件上传

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>");
        }
    }
protectedvoidbtnupload\u单击(对象发送方,事件参数e)
{
//确保图像是图像
字符串[]matchExtension={.jpg“,.png“,.gif”};
字符串[]matchMimeType={“image/jpeg”、“image/png”、“image/gif”};
if(fpSlideshow.HasFiles)
{
字符串imgName;
字符串imgExtension;
字符串imgMime;
foreach(HttpPostedFile在fpSlideshow.PostedFiles中上载的文件)
{
imgName=upLoadedFile.FileName;
imgExtension=Path.GetExtension(imgName);
imgMime=fpSlideshow.PostedFile.ContentType;
if(matchExtension.Contains(imgExtension)和&matchMimeType.Contains(imgMime))
{
fpSlideshow.SaveAs(Server.MapPath(@“~/slideshowmages/”+imgName));
字符串thePath=Server.MapPath(@“~/SlideshowImages/”+imgName.ToString();
daccess.AddImagesToSlideshow(imgName、imgExtension、imgMime、路径);
}
}
}
daccess.GetImageNames();
CreateMyStuff();
对于(int i=0;i!=daccess.dsTEST.Tables[0].Rows.Count;i++)
{
Response.Write(“
”+daccess.dsTEST.Tables[0]。行[i][“ImageName]”。ToString()+“
”+ daccess.dsTEST.Tables[0]。行[i][“ImageType”]。ToString()+“
”+ daccess.dsTEST.Tables[0]。行[i][“ImageMime”]。ToString()+“
”+ daccess.dsTEST.Tables[0]。行[i][“ImageUrl”]。ToString()+“
”; } }
CreateMyStuff方法

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;
                }
            }
        }
    }
private void CreateMyStuff()
{
//当前行计数。
int rowCtr;
//每行(列)的单元格总数。
int cellCtr;
//统计数据集中的行数
int rN=daccess.dsTEST.Tables[0].Rows.Count;
对于(rowCtr=0;rowCtr
fpSlideshow.SaveAs(Server.MapPath(@"~/SlideshowImages/" + imgName));

似乎对
uploadedFile
没有任何依赖性。对于保存的文件,每次对同一对象使用不同的文件名调用相同的方法。如果
fpSlideshow
FileUpload
类的一个实例,那么我认为默认行为是在
PostedFiles
中保存第一个文件。在
uploadedFile
上调用
.SaveAs
应该可以纠正这个问题。

感谢您指出这一点,我将fpSlideshow更改为uploadedFile,现在它工作得非常好。