我正在尝试使用bootstrap进行文件上传,但我不知道我在做什么,所以有人可以告诉我如何执行此操作以及如何获取上传的图像预览,因为文件上传旨在上传图像到我的网站上。这是我到目前为止用HTML编写的内容。
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<form class="form-horizontal" enctype="multipart/form-data">
<div class="form-group">
<label class="control-label col-sm-2">Pic Name:</label>
<div class="col-xs-4">
<input type="text" class="form-control" name="profilepicname" />
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">Upload Pic:</label>
<div class="col-xs-3">
<input type="file" name="profilepic" />
</div>
</div>
</form>
在网页上命名图片和文件上传部分之间也有很大的空间。我怎么摆脱这个?
答案 0 :(得分:5)
带有预览的图像上传表单示例,使用Bootstrap构建,没有其他CSS,它使用jQuery AJAX查询负责上传的PHP脚本:
答案 1 :(得分:0)
Jasny Bootstrap允许您接近。
请参阅documentation。
根据此编辑您的代码:
<div class="fileinput fileinput-new" data-provides="fileinput">
<div class="fileinput-preview thumbnail" data-trigger="fileinput" style="width: 200px; height: 150px;"></div>
<div>
<span class="btn btn-default btn-file"><span class="fileinput-new">Select image</span><span class="fileinput-exists">Change</span><input type="file" name="..."></span>
<a href="#" class="btn btn-default fileinput-exists" data-dismiss="fileinput">Remove</a>
</div>
</div>
答案 2 :(得分:0)
亲爱的你可以试试这个
$(document).ready( function() {
$(document).on('change', '.btn-file :file', function() {
var input = $(this),
label = input.val().replace(/\\/g, '/').replace(/.*\//, '');
input.trigger('fileselect', [label]);
});
$('.btn-file :file').on('fileselect', function(event, label) {
var input = $(this).parents('.input-group').find(':text'),
log = label;
if( input.length ) {
input.val(log);
} else {
if( log ) alert(log);
}
});
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#img-upload').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#imgInp").change(function(){
readURL(this);
});
});
.btn-file {
position: relative;
overflow: hidden;
}
.form-group {
padding: 30px;
}
.btn-file input[type=file] {
position: absolute;
top: 0;
right: 0;
min-width: 100%;
min-height: 100%;
font-size: 100px;
text-align: right;
filter: alpha(opacity=0);
opacity: 0;
outline: none;
background: white;
cursor: inherit;
display: block;
}
.input-group {
margin-bottom: 30px;
}
#img-upload{
width: 150px;
height: 150px;
}
.as-console-wrapper {
display: none !important;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<div class="container">
<div class="col-md-6">
<div class="form-group">
<label>Upload Image</label>
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-default btn-file">
Browse… <input type="file" id="imgInp">
</span>
</span>
<input type="text" name="img" class="form-control" readonly>
</div>
<img id='img-upload'/>
</div>
</div>
</div>