html改变input file文本,html - How to change the button text of <input type="file" />? - Stack Overflow...

157

Use Bootstrap FileStyle, which is used to style the file fields of forms. It is a plugin for a jQuery-based component library called Twitter Bootstrap

Sample usage:

Include:

Via JavaScript:

$(":file").filestyle();

Via data attributes:

answered Sep 20 '13 at 4:57

2aab2de31a9def9dcc61e1027708b2e0?s=32&d=identicon&r=PG

Fernando KoshFernando Kosh

3,16811 gold badge3030 silver badges2525 bronze badges

7

1

Doesn't seem to be working for me. It still shows the brorser's default buttons. Also, the demo at github.com/markusslima/bootstrap-filestyle.git doesn't work either - all examples show the default buttons. Am I missing something?

– Ya.

Jul 2 '15 at 16:07

8

@Tony Uhh.... what makes you think Bootstrap isn't using the "wacky hack" underneath as well? :-)

– Andz

Jul 3 '15 at 8:34

@Ya can you tell what browser and it version? Tested now on Chrome Version 43.0.2357.130 (64-bit) on Linux and all examples works perfectly.

– Fernando Kosh

Jul 6 '15 at 21:16

31

Throwing a javascript library (with a plugin!) on such a small problem is an inelegant overkill.

– MKaama

Sep 12 '16 at 18:48

3

Downvoted. It is not even an answer, only a workaround that needs an extra library. The answer should intend to change the rendering behaviour for browsers.

– wdetac

Jun 12 '18 at 2:58

114

You can put an image instead, and do it like this:

HTML:

uploadButton.png

JQuery:

$("#upfile1").click(function () {

$("#file1").trigger('click');

});

CAVEAT:

In IE9 and IE10 if you trigger the onClick in a file input via javascript, the form will be flagged as 'dangerous' and cannot be submitted with javascript, not sure if it can be submitted traditionally.

f03e4e34ddeeeb5854753e1f382c1d84?s=32&d=identicon&r=PG

Charlie74

2,8111212 silver badges2222 bronze badges

answered Dec 29 '11 at 11:58

ef69891008cee8ddde81d4361a4320ee?s=32&d=identicon&r=PG

ParParParPar

6,67077 gold badges4040 silver badges5252 bronze badges

6

6

This is a clever solution. Couldn't you also do this with a element?

– Code Commander

Jul 23 '13 at 17:41

1

@Code Commander -Thanks, and yes, you can do it with every element you want, but the most common is the element. Just get rid of the button+text field of the "file input"...

– ParPar

Jul 24 '13 at 21:24

2

Just a heads up to anyone using the above, some mobile browsers disable triggering file input clicks programmatically. Specifically, android 4 for the Galaxy S III

– newshorts

May 24 '14 at 19:49

I am using

– sohaiby

May 11 '15 at 12:09

your solution doesnt work. You prolly didn't set border, margin, padding and background-color or or else it would be obvious

– yan bellavance

Mar 29 '18 at 3:37

110

Hide the file input. Create a new input to capture a click event and forward it to the hidden input:

cb95a90e02fd5056cde9594582222234?s=32&d=identicon&r=PG&f=1

2540625

9,37255 gold badges4141 silver badges5151 bronze badges

answered Mar 27 '16 at 13:58

fe4a22aa5ba25ba7210e8361d445065a?s=32&d=identicon&r=PG

MushyPeasMushyPeas

1,95722 gold badges2525 silver badges4141 bronze badges

11

throwing in an HTA file with ie7 mode I had to remove the ; at the end.

– ATek

Jul 20 '17 at 23:28

great, don't want to include bootstrap just to change two words of text, but how would you do it if the file-input was created dynamically?

– asparism

Dec 10 '17 at 1:06

@asparism use JavaScript to manipulate the DOM, hide it by setting it's style and use before() and add the new input element?

– MushyPeas

Dec 10 '17 at 13:39

3

@MushyPeas it is the easiest way. tried to play with css but this solution is much faster and easier. thanks

– Mara

Jun 28 '18 at 19:33

2

This was the easiest way for me, too! It also allowed me to STYLE the 'mock' file-button as one of a set of other form-button options - without having to worry about displaying the odd-looking "Browse" file-button.

– Harry Mantheakis

Sep 12 '19 at 17:00

46

The "upload file..." text is pre-defined by the browser and can't be changed.

The only way to get around this is to use a Flash- or Java-based upload component like swfupload.

answered Dec 22 '09 at 5:11

zzxoB.jpg?s=32&g=1

PekkaPekka

419k129129 gold badges933933 silver badges10591059 bronze badges

3

5

Can you prove it that it can't be changed?

– user198729

Dec 22 '09 at 5:12

Or by browsing the browsers' source codes, at least for the Open Source ones.

– Pekka

Dec 22 '09 at 5:15

This could by the answer (to another question, something like "Why is the content of value, 'VALUE="C:\someFile.txt"' for an input field of type file, 'TYPE="FILE"', ignored by the browser?"): stackoverflow.com/questions/1342039

– Peter Mortensen

Jan 21 '10 at 15:47

26

Works Perfectly on All Browsers Hope it will work for you too.

HTML:

CSS:

.custom-file-input::-webkit-file-upload-button {

visibility: hidden;

}

.custom-file-input::before {

content: 'Select some files';

display: inline-block;

background: -webkit-linear-gradient(top, #f9f9f9, #e3e3e3);

border: 1px solid #999;

border-radius: 3px;

padding: 5px 8px;

outline: none;

white-space: nowrap;

-webkit-user-select: none;

cursor: pointer;

text-shadow: 1px 1px #fff;

font-weight: 700;

font-size: 10pt;

}

.custom-file-input:hover::before {

border-color: black;

}

.custom-file-input:active::before {

background: -webkit-linear-gradient(top, #e3e3e3, #f9f9f9);

}

Change content: 'Select some files'; with the text you want within ''

IF NOT WORKING WITH firefox then use this instead of input:

answered Oct 3 '15 at 6:32

PV9u6.png?s=32&g=1

Code BlackCode Black

44155 silver badges1111 bronze badges

11

Works on Safari, though

– Cullub

Dec 5 '15 at 14:34

Its working perfectly at my end on my FF 41.0.2.You are running in to cross browser compatibility issue please refer to CSS BOX MODEL to minify the issue.

– Code Black

Dec 7 '15 at 17:23

1

The problem is probably that FF is built on Gecko, not webkit. (my best guess)

– Cullub

Dec 7 '15 at 18:28

2

Choose Some File

– Code Black

Dec 7 '15 at 18:32

4

OK, Thanks! Check out this JSFiddle. If you edit your answer with that, I think it's worthy of an upvote :)

– Cullub

Dec 7 '15 at 18:49

16

Simply

Your text here

[Edit with snippet]

Your text here

answered Oct 31 '18 at 4:59

O9TzE.png?s=32&g=1

Ken KarloKen Karlo

17611 silver badge55 bronze badges

3

Try to see what I made here jsfiddle.net

– Ken Karlo

Nov 1 '18 at 4:54

Thanks, exactly what I wanted :)

– Garth Baker

May 16 '20 at 15:15

This is fine thank you

– Rao

2 days ago

13

I think this is what you want:

Your text here

answered Dec 23 '17 at 7:05

wVs5T.jpg?s=32&g=1

ChitChit

22522 silver badges1010 bronze badges

2

1

The best solution. Thanks!

– Eugene Veselov

Sep 18 '20 at 15:32

Thanks @EugeneVeselov

– Chit

Dec 11 '20 at 9:05

11

This is a JQuery plugin to change the button text of an input file element.

Example HTML:

Example JS:

$('#choose-file').inputFileText({

text: 'Select File'

});

Result:

grm5m.png

answered Jun 26 '15 at 0:35

0c29eed12709c8cc8e8cc74a7a7f6743?s=32&d=identicon&r=PG&f=1

datchungdatchung

1,6261818 silver badges1616 bronze badges

1

Not a great solution for 'multiple' files. Does not show more than one file.

– Benj Sanders

Jan 3 at 6:40

9

Your name

JS

document.getElementById("uploadBtn").onchange = function () {

document.getElementById("uploadFile").value = this.value;

};

photo.jpg?sz=32

Igor Ivancha

3,23144 gold badges2828 silver badges3939 bronze badges

answered Dec 11 '14 at 10:49

wNuT2.jpg?s=32&g=1

webskywebsky

2,56411 gold badge3030 silver badges2626 bronze badges

0

3

EDIT: I see now by the comments that you are asking about the button text, and not the file path. My bad. I'll leave my original answer below in case someone else who stumbles upon this question interprets it the way I originally did.

2nd EDIT: I had deleted this answer because I decided that I misunderstood the question and my answer was not relevant. However, comments in another answer indicated that people still wanted to see this answer so I'm undeleting it.

MY ORIGINAL ANSWER (I thought the OP was asking about the path, not the button text):

This is not a supported feature for security reasons. The Opera web browser used to support this but it was removed. Think about what would be possible if this were supported; You could make a page with a file upload input, pre-populate it with a path to some sensitive file and then auto-submit the form using javascript triggered by the onload event. This would happen too fast for the user to do anything about it.

3bPwx.jpg?s=32&g=1

Amirhossein Mehrvarzi

11.3k66 gold badges3838 silver badges6565 bronze badges

answered Dec 22 '09 at 5:11

8d1152398d84f15b76eb25f13e825cda?s=32&d=identicon&r=PG

AsaphAsaph

148k2424 gold badges184184 silver badges187187 bronze badges

3

Use for the caption

Your caption here

οnchange='optionalExtraProcessing(b1.files[0])'

accept='image/png'>

This works without any javascript. You can decorate the label to any degree of complexity, to your heart's content. When you click on the label, the click automatically gets redirected to the file input. The file input itself can be made invisible by any method. If you want the label to appear like a button, there are many solutions, e.g.:

label u {

-webkit-appearance: button;

-moz-appearance: button;

appearance: button;

text-decoration: none;

color: initial;

}

answered Sep 12 '16 at 18:37

09c33846082cd28153cc47f4e3e2695e?s=32&d=identicon&r=PG

MKaamaMKaama

1,38822 gold badges1515 silver badges2424 bronze badges

2

Only CSS & bootstrap class

browse

answered Aug 8 '18 at 8:58

photo.jpg?sz=32

mahmood kabimahmood kabi

1411111 bronze badges

2

I know, nobody asked for it but if anybody is using bootstrap, it can be changed through Label and CSS Pseudo-selector.

For changing button text:

.custom-file-label::after {

content: "What's up?";

}

For changing field text:

Drop it like it's hot

Here's a fiddle.

answered Jul 14 '20 at 7:55

photo.jpg?sz=32

actuallyakashactuallyakash

8166 bronze badges

1

Before that , add an image and it will occupy the space of

Use the following CSS to the file element

position:relative;

opacity:0;

z-index:99;

acYd0.png?s=32&g=1

Toby Speight

23.6k4747 gold badges5757 silver badges8484 bronze badges

answered Jul 11 '13 at 5:58

Kz3ig.jpg?s=32&g=1

Bibaswann BandyopadhyayBibaswann Bandyopadhyay

3,1772727 silver badges2727 bronze badges

0

1

for me it does not work the custom text with bootstrap-filestyle. It help with button decoration but text its weird to be changed, before get into wrestling with css i try the following :

$( document ).ready(function() {

$('.buttonText').html('Seleccione ficheros');

});

bootstrap-filestyle render the component as span with a class named butonText, so when document load just change the text. easy right and it must work on all browsers.

cheers

answered Apr 22 '19 at 9:04

ac7978c06394ac645bf74cbe032e91b8?s=32&d=identicon&r=PG&f=1

Boris Ch.FBoris Ch.F

15655 bronze badges

0

if you using rails and have problem apply it, I would like to add some tips from original answer posted by @Fernando Kosh

Download file zip and copy file bootstrap-filestyle.min.js ke folder app/assets/javascripts/

open your application.js and add this line below

//= require bootstrap-filestyle.min

open your coffee and add this line

$(":file").filestyle({buttonName: "btn-primary",buttonBefore: true,buttonText: " Your text here",icon: false});

answered Mar 20 '17 at 15:20

f611b152b4fa9fe2a05f4ee9b84fdcb7?s=32&d=identicon&r=PG

widjajaydwidjajayd

5,57433 gold badges2121 silver badges3333 bronze badges

0

You can simply add some css trick. you don't need javascript or more input files and i keep existing value attribute. you need to add only css. you can try this solution.

.btn-file-upload{

width: 187px;

position:relative;

}

.btn-file-upload:after{

content: attr(value);

position: absolute;

top: 0;

left: 0;

bottom: 0;

width: 48%;

background: #795548;

color: white;

border-radius: 2px;

text-align: center;

font-size: 12px;

line-height: 2;

}

answered Mar 7 '19 at 5:08

Ak66p.jpg?s=32&g=1

Udara KasunUdara Kasun

1,8451212 silver badges2222 bronze badges

0

Here is a way to "change" the text of an input with file type, with pure HTML and javascript:

*The text you want*

2d106de3dec9586b56ed7f110497689e?s=32&d=identicon&r=PG

Wai Ha Lee

7,7335454 gold badges5454 silver badges8080 bronze badges

answered Apr 14 '19 at 20:44

d1baf989a09eeb5a2ab4d9debb593def?s=32&d=identicon&r=PG&f=1

IntelarXIntelarX

7111 silver badge44 bronze badges

0

I did it like this for my project:

.btn-outlined.btn-primary {

color: #000;

}

.btn-outlined.btn-primary:active, .btn-outlined.btn-positive:active, .btn-outlined.btn-negative:active {

color:#000;

}

.btn-block {

display: block;

width: 100%;

padding: 15px 0;

margin-bottom: 10px;

font-size: 18px;

font-family: Arial, Helvetica, sans-serif;

text-align: center;

}

Your text

answered Apr 6 '20 at 7:50

AOh14Gg9n439bOA19BWPDPQ3GVx78tJdSO5Go5geqxdq9w=k-s32

KhapiKhapi

35033 silver badges55 bronze badges

0

In Bootstrap +4.5 you can simply add bellow style to your code:

.custom-file-input ~ .custom-file-label::after {

content: "Your custom text ...";

}

and your input:

Your custom placeholder ...

answered Aug 27 '20 at 23:01

photo.jpg?sz=32

Sajad AfaghiySajad Afaghiy

35644 silver badges1010 bronze badges

-1

This is an alternative solution that may be of help to you. This hides the text that appears out of the button, mixing it with the background-color of the div. Then you can give the div a title you like.

UPLOAD IMAGE

iRvKU.png?s=32&g=1

Kaiido

87.9k77 gold badges147147 silver badges197197 bronze badges

answered Jun 15 '13 at 4:49

477cdbc4f3b316935aa4bd2ead7306fd?s=32&d=identicon&r=PG

jarimosjarimos

133 bronze badges

2

In Chrome 39 I see both the UPLOAD IMAGE text and the button. Perhaps chrome's handling of file inputs has changed since you posted this?

– EricP

Dec 23 '14 at 18:26

1

@yan-bellavance what are you doing? If you think an answer is incorrect or not helpful, downvote, but in no way deface it!

– Kaiido

Mar 29 '18 at 3:49

association bonus) in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.

Not the answer you're looking for? Browse other questions tagged html input or ask your own question.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd" autoReload="true" throwExceptions="false" internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log"> <!-- optional, add some variables https://github.com/nlog/NLog/wiki/Configuration-file#variables --> <variable name="myvar" value="myvalue"/> <!-- See https://github.com/nlog/nlog/wiki/Configuration-file for information on customizing logging rules and outputs. --> <targets> <!-- add your targets here See https://github.com/nlog/NLog/wiki/Targets for possible targets. See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers. --> <!-- Write events to a file with the date in the filename. <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message}" /> --> <target xsi:type="File" name="f" fileName="logs/${shortdate}.log" archiveFileName="logs/archive.{#}.log" archiveEvery="Day" archiveNumbering="Rolling" maxArchiveFiles="30" concurrentWrites="true" keepFileOpen="false" encoding="utf-8"> <layout xsi:type="CsvLayout" delimiter="auto" withHeader="true" > <column name="time" layout="${longdate:universalTime=false}" /> <column name="threadid" layout="${threadid}"/> <column name="level" layout="${level:upperCase=true}"/> <column name="callsite" layout="${callsite:includeSourcePath=true}" /> <column name="message" layout="${message}" /> <column name="stacktrace" layout="${callsite:includeSourcePath=true}" /> <column name="exception" layout="${exception:format=ToString}"/> </layout> </target> <target xsi:type="File" name="c" layout="${threadid} ${uppercase:${level}} ${message} ${callsite} ${exception:format=tostring}" /> </targets> <rules> <!-- add your logging rules here --> <!-- Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f" <logger name="*" minlevel="Debug" writeTo="f" /> --> <logger name="*" writeTo="f,c" /> </rules> </nlog>NLog这个配置给我怎么加注释说明
06-04
在NLog配置文件中添加注释可以使用XML的注释标记`<!-- 注释内容 -->`,例如: ``` <?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd" autoReload="true" throwExceptions="false" internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log"> <!-- 定义变量 --> <variable name="myvar" value="myvalue"/> <!-- 定义日志输出目标 --> <targets> <!-- 文件日志 --> <target xsi:type="File" name="f" fileName="logs/${shortdate}.log" archiveFileName="logs/archive.{#}.log" archiveEvery="Day" archiveNumbering="Rolling" maxArchiveFiles="30" concurrentWrites="true" keepFileOpen="false" encoding="utf-8"> <layout xsi:type="CsvLayout" delimiter="auto" withHeader="true" > <column name="time" layout="${longdate:universalTime=false}" /> <column name="threadid" layout="${threadid}"/> <column name="level" layout="${level:upperCase=true}"/> <column name="callsite" layout="${callsite:includeSourcePath=true}" /> <column name="message" layout="${message}" /> <column name="stacktrace" layout="${callsite:includeSourcePath=true}" /> <column name="exception" layout="${exception:format=ToString}"/> </layout> </target> <!-- 控制台日志 --> <target xsi:type="File" name="c" layout="${threadid} ${uppercase:${level}} ${message} ${callsite} ${exception:format=tostring}" /> </targets> <!-- 定义日志输出规则 --> <rules> <logger name="*" writeTo="f,c" /> </rules> </nlog> ``` 在需要注释的地方添加注释即可,例如: ``` <!-- 定义变量 --> <variable name="myvar" value="myvalue"/> <!-- 定义日志输出目标 --> <targets> <!-- 文件日志 --> <target xsi:type="File" name="f" fileName="logs/${shortdate}.log" archiveFileName="logs/archive.{#}.log" archiveEvery="Day" archiveNumbering="Rolling" maxArchiveFiles="30" concurrentWrites="true" keepFileOpen="false" encoding="utf-8"> <layout xsi:type="CsvLayout" delimiter="auto" withHeader="true" > <column name="time" layout="${longdate:universalTime=false}" /> <column name="threadid" layout="${threadid}"/> <column name="level" layout="${level:upperCase=true}"/> <column name="callsite" layout="${callsite:includeSourcePath=true}" /> <column name="message" layout="${message}" /> <column name="stacktrace" layout="${callsite:includeSourcePath=true}" /> <column name="exception" layout="${exception:format=ToString}"/> </layout> </target> <!-- 控制台日志 --> <target xsi:type="File" name="c" layout="${threadid} ${uppercase:${level}} ${message} ${callsite} ${exception:format=tostring}" /> </targets> <!-- 定义日志输出规则 --> <rules> <logger name="*" writeTo="f,c" /> </rules> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值