成功的前端(红色禁用)

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>建筑效果图转彩色手绘</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background-color: #f2f2f7;
            color: #1d1d1f;
            text-align: center;
            margin: 0;
            padding: 0;
        }
        .container {
            width: 80%;
            margin: 40px auto;
        }
        .header {
            margin-bottom: 20px;
        }
        .header h1 {
            font-size: 28px;
            font-weight: normal;
        }
        .header p {
            font-size: 18px;
            color: #6e6e73;
        }
        .content {
            display: flex;
            justify-content: space-around;
            margin-top: 20px;
        }
        .upload-section, .result-section {
            flex: 1;
            background: #ffffff;
            border-radius: 12px;
            box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
            padding: 20px;
            margin: 0 10px;
            box-sizing: border-box;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }
        img {
            max-width: 100%;
            height: auto;
            border-radius: 8px;
            margin-bottom: 20px;
        }
        button {
            padding: 10px 20px;
            font-size: 16px;
            border: none;
            border-radius: 20px;
            cursor: pointer;
            transition: background-color 0.3s ease;
            width: 100%;
            margin-bottom: 10px;
        }
        .red-button {
            background-color: #ff0000;
            color: #ffffff;
        }
        .red-button:hover {
            background-color: #cc0000;
        }
        .disabled {
            background-color: #c0c0c0;
            color: #6e6e73;
            cursor: not-allowed;
        }
        #file-input {
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>建筑效果图转彩色手绘</h1>
            <p>使用介绍: 请在左侧框内上传一张建筑渲染图</p>
        </div>
        <div class="content">
            <div class="upload-section">
                <input type="file" id="file-input" hidden />
                <label for="file-input" class="upload-button">
                    <img id="original-image" src="/static/AI建筑原图.png" alt="AI建筑原图" />
                    <button id="upload-btn">上传参考图</button>
                </label>
                <button id="call-api-btn" class="red-button disabled" disabled>开始生成</button>
            </div>
            <div class="result-section">
                <img id="sketched-image" src="/static/AI建筑手绘.png" alt="AI建筑手绘图" />
            </div>
        </div>
    </div>
    <script>
        const fileInput = document.getElementById('file-input');
        const originalImage = document.getElementById('original-image');
        const sketchedImage = document.getElementById('sketched-image');
        const uploadButton = document.getElementById('upload-btn');
        const callApiButton = document.getElementById('call-api-btn');

        fileInput.addEventListener('change', function(event) {
            const file = event.target.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    originalImage.src = e.target.result;
                    callApiButton.classList.remove('disabled');
                    callApiButton.disabled = false;
                };
                reader.readAsDataURL(file);
            }
        });

        uploadButton.addEventListener('click', function() {
            fileInput.click();
        });

        callApiButton.addEventListener('click', async function() {
            this.classList.add('disabled');
            this.disabled = true;

            const formData = new FormData();
            formData.append('file', fileInput.files[0]);

            try {
                const response = await fetch('/upload', {
                    method: 'POST',
                    body: formData
                });

                if (!response.ok) throw new Error('服务器错误');

                const result = await response.json();
                if (result.error) {
                    alert(result.error);
                    return;
                }

                sketchedImage.src = '/static/' + result.processedImage;  // 注意这里已经适应了服务器返回的相对路径
            } catch (error) {
                console.error('Error:', error);
                alert('上传或处理失败');
            } finally {
                this.classList.remove('disabled');
                this.disabled = false;
            }
        });

        callApiButton.classList.add('disabled');
        callApiButton.disabled = true; // 初始时禁用
    </script>
</body>
</html>

 

posted @ 2024-05-04 00:06  不上火星不改名  阅读(18)  评论(0)    收藏  举报