Buy Licenses Contact Support Login/Register
v 1.7.19
v 1.7.19
Overview Examples Docs Source Download

File question - custom preview

Disable built-in preview and render custom one


                        
        
            Survey.StylesManager.applyTheme("default");
        


            
                var json = { questions: [
    { type: "file", title: "Please upload your files", name: "files", storeDataAsText: false, "allowMultiple":  true, showPreview: false, maxSize: 102400 }
]};

                window.survey = new Survey.Model(json);
            
        
            survey.onComplete.add(function(result) {
            document.querySelector('#surveyResult').textContent =
            "Result JSON:\n" + JSON.stringify(result.data, null, 3);
            });
        
function updatePreview(newValue, container) {
    container.innerHTML = "";
    (newValue || []).forEach(function(fileItem) {
        var button = document.createElement("div");
        button.className = "btn sv-btn sv-file__choose-btn";
        button.innerText = "Dounload " + fileItem.name;
        button.onclick = function() {
            alert(JSON.stringify(fileItem));
        }
        container.append(button);
    });
}

survey
.onAfterRenderQuestion
.add(function (sender, options) {
    if(options.question.getType() === "file") {
        var container = document.createElement("div");
        container.className = "my-preview-container";
        var fileElement = options.htmlElement.getElementsByClassName("sv_q_file")[0];
        if(!fileElement) {
            fileElement = options.htmlElement.getElementsByClassName("sv-file__decorator")[0];
        }
        fileElement.append(container);
        options.question.onPropertyChanged.add(function(question, options) {
            if(options.name === "value") {
                updatePreview(options.newValue, container);
            }
        });
        updatePreview(options.question.value, container);
    }
});


survey
.onUploadFiles
.add((survey, options) => {
    var formData = new FormData();
    options
        .files
        .forEach(function (file) {
            formData.append(file.name, file);
        });
    var xhr = new XMLHttpRequest();
    xhr.responseType = "json";
    xhr.open("POST", "https://surveyjs.io/api/MySurveys/uploadFiles?accessKey=<your_access_key>");
    xhr.onload = function () {
        var data = xhr.response;
        options.callback("success", options.files.map(file => {
            return {
                file: file,
                content: data[file.name]
            };
        }));
    };
    xhr.send(formData);
});

survey
.onDownloadFile
.add((survey, options) => {

    alert("Download called");

    var xhr = new XMLHttpRequest();
    xhr.responseType = "blob";
    xhr.open("GET", "https://surveyjs.io/api/MySurveys/files?name=" + options.content);
    xhr.onload = function () {
        var reader = new FileReader();
        reader.onload = function (e) {
            options.callback("success", e.target.result);
        };
        reader.readAsDataURL(new File([xhr.response], options.fileValue.name, {type: options.fileValue.type}));
    };
    xhr.send();

});


survey.data = {"files":[{"name":"TestTemplate.rtf","type":"","content":"00000000-0000-0000-0000-000000000000"},{"name":"pic_e1f5e6f901fe2fb17f78b7cfed600950.jpg","type":"image/jpeg","content":"00000000-0000-0000-0000-000000000000"}]};
        
            ReactDOM.render(<Survey.Survey model={survey}  />, document.getElementById("surveyElement"));
        

                    
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Disable built-in preview and render custom one, Reactjs Survey Library Example</title>

<meta name="viewport" content="width=device-width" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.6.0/polyfill.js"></script>
    <script src="https://unpkg.com/react@15/dist/react.js"></script>
    <script src="https://unpkg.com/react-dom@15/dist/react-dom.js"></script>
    <script src="https://unpkg.com/@babel/standalone@7.2.5/babel.min.js"></script>
<script src="https://surveyjs.azureedge.net/1.7.19/survey.react.js"></script>
<link href="https://surveyjs.azureedge.net/1.7.19/survey.css" type="text/css" rel="stylesheet" />
    <link rel="stylesheet" href="./index.css">

</head>
<body>
    
            <div id="surveyElement" style="display:inline-block;width:100%;">
            </div>
    <div id="surveyResult"></div>

<script type="text/babel" src="./index.js"></script>

</body>
</html>

.my-preview-container {
  margin-top: 2em;
}

.my-preview-container .sv-file__choose-btn {
  margin: 0 1em;
}

.btn {
    border: 1px solid lightgray;
    cursor: pointer;
    padding: 4px 8px;
    display: inline-block;
}