【问题标题】:Uploading Image with Button Click点击按钮上传图片
【发布时间】:2014-05-26 14:28:25
【问题描述】:

我必须通过单击图像按钮来上传图像。 我尝试使用以下代码。

<head runat="server">
<title></title>
<script>
    function browse() {
        document.getElementById('<%= FileUpload1.ClientID %>').click();
 }
  </script>
   </head>
<body>
<form id="form1" runat="server">
<div>
  <asp:toolkitscriptmanager ID="ToolkitScriptManager1" runat="server">
  </asp:toolkitscriptmanager>           
  <asp:FileUpload ID="FileUpload1" runat="server" />
  <asp:ImageButton ID="ImageButton1" runat="server" OnClientClick="browse();"
  ImageUrl="logo.gif" />
 </div>
 </form>
 </body>

在图像按钮上单击浏览窗口打开。但是当我单击图像并打开它时,该图像路径在上传控件文本框中不可见,就像我们通常使用没有图像按钮的上传控件一样。这意味着上传 control.Hasfile 为 false 谁能帮我解决这个问题?

提前致谢

【问题讨论】:

    标签: c# javascript image file-upload


    【解决方案1】:

    当您使用文件上传器浏览图像时,路径会自动出现在文本框中。

    不需要额外的眉毛或图像按钮。

    默认提供浏览按钮。

    您可以在以下链接中学习文件上传器的图示:

    http://www.devmanuals.com/tutorials/ms/aspdotnet/fileupload.html

    如果要保存图像,请在代码中使用此功能:

    protected void btnUploadClick(object sender, EventArgs e) 
    { 
        HttpPostedFile file = Request.Files["myFile"]; 
        if (file != null && file.ContentLength ) 
        { 
            string fname = Path.GetFileName(file.FileName); 
            file.SaveAs(Server.MapPath(Path.Combine("~/App_Data/", fname))); 
        } 
    } 
    

    【讨论】:

    • 我需要添加一张图片,并且我希望打开浏览窗口。我做了,但它没有被上传
    • @SandraDsouza plz 如果要保存该图像,请在代码中使用上述功能
    【解决方案2】:

    图片按钮使用以下代码:

    OnClientClick="browse(); return false;"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-22
      • 2011-12-01
      • 2020-05-05
      • 1970-01-01
      • 1970-01-01
      • 2022-12-13
      相关资源
      最近更新 更多