如何更改FileUpload控件中的按钮文本并将值设置为文本框

时间:2013-09-30 08:30:02

标签: c# asp.net .net

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

enter image description here

  1. 我想将按钮的文本从默认的“浏览”更改为“上传”。
  2. 当用户返回此页面时,我想显示他/她之前在文本框中上传的图像名称。
  3. 经过一些搜索后,我发现无法编辑按钮名称和文本框的设置值。

    所以我开发了以下编码。

    .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返回空。有些人指导我。

3 个答案:

答案 0 :(得分:0)

你可以这样做。

<强> .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中提交表单的方式,我只是用最快的方式。表格将在用户选择文件后提交。

答案 1 :(得分:0)

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

答案 2 :(得分:-1)

使用两个js文件http://the-echoplex.net/demos/upload-file/file-upload.jshttp://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

下载示例项目