single_file_page.hbs 2.35 KB
<div class="pageheader">
    <div class="media">
        <div class="pageicon pull-left">
            <i class="fa fa-th-list"></i>
        </div>
        <div class="media-body">
            <ul class="breadcrumb">
                <li><a href=""><i class="glyphicon glyphicon-home"></i></a></li>
                <li>静态资源</li>
            </ul>
            <h4>单文件上传</h4>
        </div>
    </div>
    <!-- media -->
</div>
<!-- pageheader -->
<div class="contentpanel">
    <div class="form">
        <div class="form-group">
            <label for=""></label>   
            <div class="input-group">
                <span class="input-group-addon">{{domain}}/</span>
                <input type="text" class="form-control" id="actPath" aria-describedby="basic-addon3" placeholder="请输入文件路径,例:20171030/v2.4.8">
            </div>
        </div>
        <div class="form-group">
            <label for="file">选择文件</label>
            <input type="file" class="form-control" name="file" id="fileInput">
        </div>
        <input type="hidden" id="cover" name="cover" value="29m34gs6f5">
        <div class="form-group">
            <button class="btn btn-default" id="upLoadBtn">上传</button>
            <span id="resultMsg"></span>
        </div>
    </div>
</div>

<script>
    $('#upLoadBtn').on('click', function(){
        $('#upLoadBtn').attr('disabled', 'disabled');

        var file = $('#fileInput').prop('files')[0];
        var actPath = $('#actPath').val();
        var formData = new FormData();
        var cover = $('#cover').val();

        formData.append('file', file);
        formData.append('actPath', actPath);
        formData.append('cover', cover);

        $.ajax({
            type: 'POST',
            url: '/files/single-file-upload',
            cache: false,
            data: formData,
            dataType: 'json',
            processData: false,
            contentType: false,
            complete: function() {
                $('#upLoadBtn').removeAttr('disabled');
            },
            success: function(result) {
               if (result.code === 200) {
                   $('#resultMsg').html(`<a target="_blank" href="${result.data.url}">${result.data.url}</a>`);
               } else {
                   $('#resultMsg').html(result.message);
               }
            }
        });
    });
</script>