如何在上传时显示图像预览

时间:2017-05-14 04:39:53

标签: c# html asp.net visual-studio

我想要的是,当我在显示图像名称时立即从FileUpload标签上传图像时,在其下方的图像框中显示正在上传的图像的预览。

文件上传标签

 <asp:FileUpload ID="FileUpload1" CssClass="form-control" runat="server" width="280px" />

图像标记

 <asp:Image ID="Image1" runat="server" Height="221px" Width="212px" />

背后的代码是

 string path = Server.MapPath("~/Images/");
        if(FileUpload1.HasFile)
        {
            string extension = Path.GetExtension(FileUpload1.FileName);
            FileUpload1.SaveAs(path + FileUpload1.FileName);
            string name = "~/Images/" + FileUpload1.FileName;
            Image1.ImageUrl = name;
        }

“Images”文件夹位于主机上。图像由用户上传。在同一页面(表格)上,应显示图像的预览。

1 个答案:

答案 0 :(得分:0)

只需使用此代码即可在我的项目中使用。

`

   if (uploadfilee.HasFile)
   {
        string str = uploadfilee.FileName;

        uploadfilee.PostedFile.SaveAs(Server.MapPath(".") + "/images/" + str);
        string path = str.ToString();
        image1.Imageurl = path;
   }

`