Question T734
Visible to All Users

File Upload

created 7 years ago

I have a question regarding the file upload question on the survey. How do I access the file that has been uploaded? For instance, I would like to save the file the user has selected in the file structure of the web server.

Answers

created 7 years ago

Hi,
Here is an event onUploadFiles: https://surveyjs.io/Documentation/Library/?id=surveymodel#onUploadFiles and onDownloadFile: https://surveyjs.io/Documentation/Library/?id=surveymodel#onDownloadFile
Here is the example: https://embed.plnkr.co/QJRlBE/ that may help you.

Thank you,
Andrew
SurveyJS Team

    Show previous comments (7)

      Hello,

      The files is an array of the File objects. In order to get file content, you should use the FileReader.
      You can find more information in the W3C FileAPI specification - https://www.w3.org/TR/FileAPI/

      Thanks, Serge
      SurveyJS Team

      S S
      surveyprogrammer 7 years ago

        So, I finally got the process working as I wanted to upload documents (even larger documents). I now need to get this working as part of a loop, but that should be pretty straight forward. One more question, however. The file upload occurs as soon as the file is selected. After ensuring that the file is uploaded, the Clean button appears. If you click the Clean button, the file still remains on the server. Is there a function that can be called when the Clean button is clicked that I can use to write code to remove the file from the server? Or, is there a way to hold off the upload process until the Complete button is clicked at the end of the survey?

        JS file
        survey.onUploadFiles.add(function(sender, options) {
        var files = options.files[0];
        var projname = survey.getValue('projname');
        var form = new FormData();
        form.append("filename",files);
        form.append("projname",projname);
        var xhr = new XMLHttpRequest();
        xhr.open('POST', 'upload.php', true);
        xhr.send(form);
        setTimeout(()=>{
        options.callback("success", [
        {
        file: options.files[0]
        }
        ]);
        }, 2000);
        });

        PHP file
        $projname = $_POST['projname'];
        $target_dir = "/var/www/request/docs/".$projname."/";
        $target_file = $target_dir.basename($_FILES["filename"]["name"]);
        mkdir ($target_dir,0755,true);
        move_uploaded_file($_FILES["filename"]["tmp_name"], $target_file);

          I've implemented corresponding event in the survey model - onClearFiles. It gets the file question value and you can use as an argument for the server API call to remove the files from the server storeage. You should call the options callback with the "success" status to clear the quesion value:

          JavaScript
          options.callback("success", options.value);

          This functionality will be available in the nearest minor update.

          Thanks, Serge
          SurveyJS Team