【问题标题】:How to change Text of button in FileUpload control and set value to textbox如何更改 FileUpload 控件中按钮的文本并将值设置为文本框
【发布时间】:2013-09-30 08:30:02
【问题描述】:

您好,我想使用 c# 上传文件,但想显示如下图。

  1. 我想将按钮的文本从默认的“浏览”更改为“上传”。
  2. 当用户返回此页面时,我想在文本框中显示他/她之前上传的图片名称。

经过一番搜索,我发现无法编辑按钮名称并为文本框设置值。

所以我开发了下面的编码。

.ASPX

 <input runat="server" id="File1" type="file" style=" visibility:hidden;" />
                      <input id="Text1" type="text" runat="server" />
                      <asp:Button ID="Button1" OnClientClick="fireFileClick()" runat="server" Text="Upload" />

Js

function fireFileClick() {
            var objfile = document.getElementById("<%= File1.ClientID %>");
            objfile.click();


            var objTextBox = document.getElementById("<%= Text1.ClientID %>");
            objTextBox.value = objfile.value;
        }

.cs

private void SetPicture()
{
string strFileName = System.IO.Path.GetFileName(File1.PostedFile.FileName);
            string strExtnt = strFileName.Substring(strFileName.LastIndexOf('.') + 1).ToUpper();
            System.Drawing.Image UploadedImage = System.Drawing.Image.FromStream(File1.PostedFile.InputStream);
            float UploadedImageWidth = UploadedImage.PhysicalDimension.Width;
            float UploadedImageHeight = UploadedImage.PhysicalDimension.Height;
            if (UploadedImageWidth < 185 && UploadedImageHeight < 51)
            {
}
}

问题在于 .CS 我没有获取文件名和路径。变量 strFileName , strExtnt 返回空。一些身体引导我。

【问题讨论】:

    标签: c# asp.net .net


    【解决方案1】:

    你可以这样做。

    .aspx:

    <input id="File1" type="file" runat="server" style="visibility: hidden"
        onchange="this.form.submit();" />
    <input id="Button2" type="button" onclick="fireFileClick();" value="Upload" />
    

    JavaScript:

    function fireFileClick() {
        document.getElementById("<%= File1.ClientID %>").click();
    }
    

    .cs:和你的一样。

    注意:请随意更改您在 JavaScript 中提交表单的方式,我只是使用了最快的方式。表单将在用户选择文件后提交。

    【讨论】:

      【解决方案2】:

      查看此链接:ref
      基本上使用两个输入控件,隐藏实际输入上传控件,并将样式应用于非文件上传控件。很棒的酱汁!

      【讨论】:

        【解决方案3】:

        使用两个js文件http://the-echoplex.net/demos/upload-file/file-upload.js和http://the-echoplex.net/demos/upload-file/jelly/min.js。并添加file-upload.css文件。你的示例 aspx 文件是,

        <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
         <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
         <html xmlns="http://www.w3.org/1999/xhtml">  
         <script src="script/jelly.js" type="text/javascript"></script>  
           <style type="text/css">  
         /****************** Start page styles ********************************************/  
         body {  
              background: #DFA01B;  
              font-family: arial, sans-serif;  
              font-size: 11px;  
              }  
         #wrap {  
              max-width: 600px;  
              margin: 30px auto;  
              background: #fff;  
              border: 4px solid #FFD16F;  
              -moz-border-radius: 15px;  
              -webkit-border-radius: 15px;  
              border-radius: 15px;  
              padding: 20px;  
           }  
         .field {   
              padding: 0 0 1em;   
              }  
         </style>  
         <head runat="server">  
           <title></title>  
         </head>  
         <body>  
           <form id="form1" runat="server">  
           <div id="wrap">  
         <form enctype="multipart/form-data" action="#" method="post">  
              <div class="field">  
                   <label class="file-upload">  
                        <span><strong>Put YOUR TEXT</strong></span>  
                        <%--<input type="file" name="uploadfile" onclintclick="test_load()" />--%>  
               <asp:FileUpload  
                 ID="FileUpload1" name="uploadfile" runat="server"   
               ondatabinding="FileUpload1_DataBinding" />  
                   </label>  
              </div>  
         </form>  
         </div><!--/ wrap -->  
           <script src="script/file-upload.js" type="text/javascript"></script>  
           </form>  
         </body>  
         </html>  
        

        和 CSS 文件,

        body {
        }
        /* 
        
            As this stylesheet is lazy loaded these styles only apply if JavaScript is enabled
        
        */  
        .file-upload {
            overflow: hidden;
            display: inline-block;
            position: relative; 
            vertical-align: middle;
            text-align: center;
        
            /* Cosmetics */
            color: #fff;
            border: 2px solid #2FA2FF;
            background: #6FBEFF;
        
            /* Nice if your browser can do it */
            -moz-border-radius: 8px;
            -webkit-border-radius: 8px;
            border-radius: 8px;
            text-shadow: #000 1px 1px 4px;
            }
        
        .file-upload:hover { 
            background: #2FA2FF; 
            }
        
        .file-upload.focus { 
            outline: 2px solid yellow;
            }
        
        .file-upload input {
            position: absolute;
            top: 0;
            left: 0;
            margin: 0;
            font-size: 70px;
        
            /* Loses tab index in webkit if width is set to 0 */
            opacity: 0;
            filter: alpha(opacity=0);
            }
        
        .file-upload strong { 
            font: normal 1.75em arial,sans-serif; 
            }   
        
        .file-upload span {
            position: absolute;
            top: 0;
            left: 0;
            display: inline-block;
        
            /* Adjust button text vertical alignment */
            padding-top: .45em;
            }
        
        /* Adjust the button size */    
        .file-upload { height: 3em; }
        .file-upload,
        .file-upload span { width: 14em; }  
        
        .file-upload-status {
            margin-left: 10px;
            vertical-align: middle;
            padding: 7px 11px;
            font-weight: bold;  
            font-size: 16px;
            color: #888;
            background: #f8f8f8;
            border: 3px solid #ddd;
            }
        

        您可以在changedfileuploadbutton text下载示例项目

        【讨论】:

          猜你喜欢
          • 2010-09-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-07
          • 2014-01-07
          • 2012-03-01
          • 1970-01-01
          相关资源
          最近更新 更多