• js原生上传图片


    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport"
    content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    </head>
    <body>

    <!-- 上传单个图片 -->
    <input type="file" id="upload">

    <!-- 上传多个图片 -->
    <input type="file" multiple id="uploadMultiple">


    <script src="home/js/jquery-3.2.1.min-1.js"></script>
    <script>
    document.getElementById('upload').addEventListener('change', function (event) {
    var $file = event.currentTarget;

    var formData = new FormData();

    var file = $file.files;

    formData = new FormData();

    formData.append(file[0].name, file[0]);

    $.ajax({
    url: '/upload',
    type: 'POST',
    dataType: 'json',
    data: formData,
    contentType: false,
    processData: false
    })
    .done(data => {
    console.log(data)
    $('#box').append(`<div class="photo-item"><img class="item-image" width="100%" height="100%" src="${data.url}"/></div>`);
    })
    .fail(data => {
    console.log(data)
    })
    });

    document.getElementById('uploadMultiple').addEventListener('change', function (event) {
    var $file = event.currentTarget;

    var formData = new FormData();

    var file = $file.files;

    for (var i = 0; i < file.length; i++) {
    formData = new FormData();
    formData.append(file[i].name, file[i]);
    $.ajax({
    url: '/upload',
    type: 'POST',
    dataType: 'json',
    data: formData,
    contentType: false,
    processData: false
    })
    .done(data => {
    console.log(data)
    $('#box').append(`<div class="photo-item"><img class="item-image" width="100%" height="100%" src="${data.url}"/></div>`);
    })
    .fail(data => {
    console.log(data);
    });
    }
    });
    </script>

    </body>
    </html>
  • 相关阅读:
    FastDFS介绍
    SwiftUI 中使用SDWebImageSwiftUI加载网络图片
    SwiftUI 中使用BBSwiftUIKit开源库实现上拉加载和下拉刷新
    SwiftUI 中使用ScrollView+LazyVStack代替List
    SwiftUI 动画
    SwiftUI 中实现省市区选择器
    SwiftUI 中Slider的使用
    SwiftUI 中Stepper的使用
    SwiftUI 中通过Toggle实现单选框和复选框效果
    SwiftUI 中加载bundle中的图片
  • 原文地址:https://www.cnblogs.com/xushan03/p/15015501.html
Copyright © 2020-2023  润新知