上传多张图片并显示

时间:2018-06-23 14:24:16

标签: c# asp.net linq web

我不会从Cookie的FileUpload存储主题中上传恶意图片,并在我仅将最后一张照片更新为更新肛门的同时在更新标签中显示这些图片

这是C#中的代码:

protected void addphoto_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile )
        {
            for (int i = 0; i < FileUpload1.PostedFiles.Count; i++)
            {
                string vale = Guid.NewGuid().ToString() + ".png";
                string path = Server.MapPath("").Replace("user", "pimg") + "\\" + vale;
                FileUpload1.SaveAs(path);
                b = "~/pimg/" + vale;
                //string test = "~/pimg/d13526f4-9713-48fa-bb5d-a37f27b0366e.png";
                if (Request.Cookies["aa"] == null)
                {
                    Response.Cookies["aa"].Value = b;
                }
                else
                {
                    Response.Cookies["aa"].Value = Request.Cookies["aa"].Value + "|" + b;
                }
            }

        }

if (Request.Cookies["aa"] != null)
        {
            s = Convert.ToString(Request.Cookies["aa"].Value);
            string[] strarr = s.Split('|');
            foreach (var item in strarr)
            {
                //string html = "<a class='btn btn-primary' style='padding: 10px; margin: 10px' href='deletitem?myid=" + item.ProdectID + "'>" + item.Name + "</a>" + "";
                Image img = new Image();
                img.ImageUrl = item.ToString();
                img.Height = 150;
                img.Width = 100;
                UpdatePanel2.ContentTemplateContainer.Controls.Add(img);

            }

}
}

1 个答案:

答案 0 :(得分:0)

尝试以下代码。它将起作用。

//This is your .aspx code. The AllowMultiple on FileUpload only works in .NET Framework 4.5+
<form id="form1" runat="server" enctype="multipart/form-data">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="true" />
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
            </ContentTemplate>
            <Triggers>
                <asp:PostBackTrigger ControlID="Button1" />
            </Triggers>
        </asp:UpdatePanel>
    </div>
</form>

//This is your code behind
protected void Button1_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFiles)
    {
        foreach (var file in FileUpload1.PostedFiles)
        {
            string value = Guid.NewGuid().ToString();
            string extension = new FileInfo(file.FileName).Extension;

            string path = Server.MapPath("~/pimg/" + value + extension);
            string virtualPath = "/pimg/" + value + extension;

            System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream);
            img.Save(path);

            if (Request.Cookies["aa"] == null)
            {
                Response.Cookies["aa"].Value = virtualPath;
            }
            else
            {
                Response.Cookies["aa"].Value = Request.Cookies["aa"].Value + "|" + virtualPath;
            }
        }
    }

    if (Request.Cookies["aa"] != null)
    {
        string[] picArray = Convert.ToString(Request.Cookies["aa"].Value).Split('|');
        foreach (var item in picArray)
        {
            Image img = new Image();
            img.ImageUrl = item.ToString();
            img.Height = 150;
            img.Width = 100;
            UpdatePanel1.ContentTemplateContainer.Controls.Add(img);
        }
    }
}

请告诉我它是否不适合您。