我尝试使用Rails创建一个按钮,使用Rails上传图片。我试过这个:
<input class="tiny round disabled button" name="picture[picture]" type="file">
不幸的是,它没有工作,并创建了两个不同的按钮,让您选择一张图片。有什么我需要专门为文件字段做的吗?
答案 0 :(得分:4)
我发现这个资源对样式输入类型=“文件”非常有帮助:
http://www.quirksmode.org/dom/inputfile.html
这是出了名的困难,但这实际上涉及将实际输入与应用了样式的伪造输入分层。
<div class="file-inputs">
<input type="file" class="hidden-input">
<div class="fake-input">
<input>
<img src="images/YOURBUTTON.png">
</div>
</div>
使用以下CSS:
div.file-inputs {
position: relative;
}
div.hidden-input {
position: absolute;
top: 0px;
left: 0px;
z-index: 1;
}
input.file {
position: relative;
text-align: right;
-moz-opacity:0 ;
filter:alpha(opacity: 0);
opacity: 0;
z-index: 2;
}
答案 1 :(得分:1)
使用html css和js
的自定义文件上传按钮Html代码:
<div class="custom-file-upload">
<!--<label for="file">File: </label>-->
<input type="file" id="file" name="myfiles[]" multiple />
</div>
<强> CSS:强>
.custom-file-upload-hidden {
display: none;
visibility: hidden;
position: absolute;
left: -9999px;
}
.custom-file-upload {
display: block;
width: auto;
font-size: 16px;
margin-top: 30px;
}
.custom-file-upload label {
display: block;
margin-bottom: 5px;
}
.file-upload-wrapper {
position: relative;
margin-bottom: 5px;
}
.file-upload-input {
width: 300px;
color: #fff;
font-size: 16px;
padding: 11px 17px;
border: none;
background-color: #c0392b;
-moz-transition: all 0.2s ease-in;
-o-transition: all 0.2s ease-in;
-webkit-transition: all 0.2s ease-in;
transition: all 0.2s ease-in;
float: left;
/* IE 9 Fix */
}
.file-upload-input:hover, .file-upload-input:focus {
background-color: #ab3326;
outline: none;
}
.file-upload-button {
cursor: pointer;
display: inline-block;
color: #fff;
font-size: 16px;
text-transform: uppercase;
padding: 11px 20px;
border: none;
margin-left: -1px;
background-color: #962d22;
float: left;
/* IE 9 Fix */
-moz-transition: all 0.2s ease-in;
-o-transition: all 0.2s ease-in;
-webkit-transition: all 0.2s ease-in;
transition: all 0.2s ease-in;
}
.file-upload-button:hover {
background-color: #6d2018;
}
JS CODE: http://codepen.io/wallaceerick/pen/fEdrz检查此内容以获取完整的详细信息
答案 2 :(得分:1)
诀窍是做一些看起来像这样的事情:
<强> HTML 强>
<button class="file-upload">Upload
<input type="file" name="files" />
</button>
<强> CSS 强>
button.file-upload > input[type='file'] {
cursor: pointer;
position: absolute;
font-size: 0;
top: 0;
left: 0;
opacity: 0;
height: 100%;
width: 100%;
}
使用基础v5.5.2:http://codepen.io/soyuka/pen/xGMPKJ