美文网首页jQuery学习笔记
基于jQuery.ajax()的文件上传

基于jQuery.ajax()的文件上传

作者: 小人物的秘密花园 | 来源:发表于2018-05-09 14:34 被阅读6次

    概述

    最近在做的项目中由于页面简单,不希望采用插件来实现文件的上传,而是通过$.ajax()来实现;

    代码

    HTML

    <div class="toolbar">
                <div class="t-btn">
                    <input type="text" placeholder="Select file..." class="file-path">
                    <button class="remove active"><i class="fa fa-trash-o fa-lg"></i>Remove</button>
                    <button class="upload active"><i class="fa fa-upload" aria-hidden="true"></i>Upload</button>
                   
                </div>
                <form class="t-btn upload-file" enctype="multipart/form-data" id="uploadForm">
                 <button class="browser">
                    <i class="fa fa-folder-open-o" aria-hidden="true"></i>
                    <#--  <span></span>  -->
                    <input type="file" name="file" id="uploadFile">
                    Browser file
                </button>
                </form>
    </div>
    

    CSS

    .toolbar {
        width: 100%;
        margin: 10px 0;
    }
    
    .t-btn {
        display: inline-block;
        margin-right: 3px;
    }
    
    .t-btn input[type="text"],
    .t-btn button {
        position: relative;
        display: inline-block;
        color: #333;
        background-color: #f3f3f3;
        overflow: hidden;
        text-decoration: none;
        padding: 6px 12px;
        margin-bottom: 0;
        font-size: 14px;
        font-weight: 400;
        line-height: 1.42857143;
        text-align: center;
        white-space: nowrap;
        vertical-align: middle;
        -ms-touch-action: manipulation;
        touch-action: manipulation;
        cursor: pointer;
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
        background-image: none;
        border: 1px solid transparent;
        border-radius: 4px;
        border-color: #ccc;
        cursor: pointer;
    }
    
    .t-btn input[type="text"]:hover,
    .t-btn button:hover {
        color: #333;
        background-color: #dcdcdc;
        border-color: #adadad;
    }
    
    .t-btn button.remove,
    .t-btn button.upload {
        margin-left: -6px;
        border-radius: 0 4px 4px 0;
    }
    .t-btn input[type="file"] {
        opacity: 0;
        position: absolute;
        left: 0;
        top: 0;
        cursor: pointer;
    }
    
    .t-btn button i {
        margin-right: 2px;
    }
    
    .t-btn button.active {
        display: none;
    }
    
    .upload-file {
        margin-left: -5px;
    }
    
    .upload-file .browser {
        border-radius: 0 4px 4px 0;
    }
    
    .file-path {
        min-width: 400px!important;
        background: transparent!important;
        border-radius: 0 4px 4px 0!important;
        text-align: left!important;
    }
    
    .file-path::-webkit-input-placeholder {
        color: #f00;
        font-weight: 600;
    }
    
    .file-path:-moz-placeholder {
        color: #f00;
        font-weight: 600;
    }
    
    .file-path::-moz-placeholder,
    textarea::-moz-placeholder {
        color: #f00;
        font-weight: 600;
    }
    
    .file-path:-ms-input-placeholder {
        color: #f00;
        font-weight: 600;
    }
    
    

    JS

     $('.upload').on('click', function(e) {
            e.preventDefault();
            var that = this;
            var fileObj = document.getElementById("uploadFile").files[0];
            var formFile = new FormData();
            //formFile.append("action", "the path of upload file");
            formFile.append("file", fileObj);
            formFile.append("enctype", "multipart/form-data");
            $.ajax({
                url: "the path of upload file",
                type: "post",
                data: formFile,
                //enctype: "multipart/form-data",
                dataType: 'json',
                cache: false, //上传文件无需缓存
                processData: false, //用于对data参数进行序列化处理 这里必须false
                contentType: false, //必须
                success: function(data) {
                   // do sth
                },
                error: function(res) {
    
                }
            });
    
        });
    

    结果

    效果图.png

    相关文章

      网友评论

        本文标题:基于jQuery.ajax()的文件上传

        本文链接:https://www.haomeiwen.com/subject/qawnrftx.html