【问题标题】:How to display the image preview when its uploaded上传时如何显示图像预览
【发布时间】:2017-05-14 15:00:52
【问题描述】:

我想要的是,当我从 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;
        }

“图像”文件夹位于主机上。图片由用户上传。并且在同一页面(表单)上应该显示图像的预览。

【问题讨论】:

  • 你有什么问题?它没有显示图像或其他任何内容吗?
  • 耶!它不会自动显示图像。我不得不为此添加一个额外的事件。就像按下按钮一样,将显示上传文件的预览。

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


【解决方案1】:

只需使用它在我的项目中工作的这段代码。

`

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

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

`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    相关资源
    最近更新 更多