Y Tools

Upload File

תצוגה חיה

בחר קובץ להעלאה...

קוד להטמעה

1. HTML / Contact Form 7
<span class="upload-container">
    <!-- Contact Form 7 Tag Example: [file the-file filetypes:pdf|doc|docx|txt class:file-input] -->
    <input type="file" name="my-file" class="file-input">
    
    <span class="upload-btn-wrap">
        <svg class="upload-icon" width="24" height="24" viewBox="0 0 24 24" aria-hidden="true"><path d="M12 16V2M12 2L15 5M12 2L9 5M20 16V18C20 19.1046 19.1046 20 18 20H6C4.89543 20 4 19.1046 4 18V16" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" fill="none"/></svg>
        <span class="file-input-val-text" data-default-text="בחר קובץ להעלאה...">בחר קובץ להעלאה...</span>
        <button type="button" class="delete-file" title="מחק קובץ">&times;</button>
    </span>
</span>
2. CSS
.upload-container {
    position: relative;
    z-index: 1;
    display: inline-block;
}
.upload-btn-wrap {
    background-color: rgba(255, 255, 255, 0.03);
    display: inline-flex;
    width: 300px;
    max-width: 100%;
    align-items: center;
    gap: 12px;
    padding: 12px 24px;
    border: 2px dashed rgba(255, 255, 255, 0.15);
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.3s ease;
    backdrop-filter: blur(8px);
}
.upload-btn-wrap:hover {
    border-color: #71F494;
    background-color: rgba(255, 255, 255, 0.06);
}
.upload-btn-wrap.active {
    border-style: solid;
    border-color: #71F494;
    background-color: rgba(113, 244, 148, 0.1);
    box-shadow: 0 0 15px rgba(113, 244, 148, 0.15);
}
.upload-btn-wrap.error {
    border-color: #ef4444;
    background-color: rgba(239, 68, 68, 0.1);
}
.file-input-val-text {
    font-size: 15px;
    color: #f8fafc;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.upload-icon {
    width: 20px;
    height: 20px;
    flex-shrink: 0;
    stroke: #94a3b8;
    transition: stroke 0.3s ease;
}
.upload-btn-wrap:hover .upload-icon path,
.upload-btn-wrap.active .upload-icon path {
    stroke: #71F494;
}
.delete-file {
    display: inline-flex;
    background: rgba(239, 68, 68, 0.15);
    color: #ef4444;
    border: none;
    border-radius: 50%;
    width: 24px;
    height: 24px;
    font-size: 18px;
    line-height: 1;
    cursor: pointer;
    align-items: center;
    justify-content: center;
    margin-right: auto;
    z-index: 1;
    transition: all 0.2s;
}
html[dir="rtl"] .delete-file { margin-right: auto; margin-left: 0; }
html:not([dir="rtl"]) .delete-file { margin-left: auto; margin-right: 0; }
.delete-file:hover { background: #ef4444; color: #fff; }
.upload-container:not(.active) .delete-file { display: none; }
.file-input {
    position: absolute;
    inset: 0;
    opacity: 0;
    cursor: pointer;
    width: 100%;
    height: 100%;
}
.file-input:focus-visible + .upload-btn-wrap { outline: 2px solid #71F494; }
.error-tip { color: #ef4444; font-size: 13px; margin-top: 8px; display: block; }
3. JavaScript
(() => {
    function resetUploadUI(wrapper) {
        const fileInput = wrapper.querySelector('.file-input');
        const textDisplay = wrapper.querySelector('.file-input-val-text');
        const defaultText = textDisplay.getAttribute('data-default-text');
        
        if (fileInput) fileInput.value = '';
        textDisplay.textContent = defaultText;
        wrapper.classList.remove('active', 'error');
    }

    document.querySelectorAll('.upload-container').forEach(wrapper => {
        const fileInput = wrapper.querySelector('.file-input');
        const textDisplay = wrapper.querySelector('.file-input-val-text');
        const deleteBtn = wrapper.querySelector('.delete-file');

        if (!fileInput) return;

        fileInput.addEventListener('change', (e) => {
            if (e.target.files[0]) {
                textDisplay.textContent = e.target.files[0].name;
                wrapper.classList.add('active');
                wrapper.classList.remove('error');
            } else {
                resetUploadUI(wrapper);
            }
        });

        deleteBtn.addEventListener('click', (e) => {
            e.preventDefault();
            e.stopPropagation();
            resetUploadUI(wrapper);
        });
    });

    // Reset UI after Contact Form 7 successful submission
    document.addEventListener('wpcf7mailsent', () => {
        document.querySelectorAll('.upload-container').forEach(wrapper => {
            resetUploadUI(wrapper);
        });
    }, false);
})();