Hi Team,
We have set storeDataAsText: false
for the file upload question type.
We are storing the uploaded files in our database using the onUploadFiles
event, and the upload process is working fine.
However, when I try to download the uploaded file from the server, it downloads the wrong type of file. Additionally, I have added an onDownloadFile
event, but it is not triggering when I click to download the file.
Locally, I'm also getting an error stating that the file wasn't available on the site.
Here are the ways I tried to fire the onDownloadFile
event handler:
survey.onDownloadFile.add(downloadFileFromSurvey);
const downloadFileFromSurvey = React.useCallback((sender, options) => {
console.log({sender, options});
});
survey.onDownloadFile.add((survey, options) => {
// console.log({survey, options});
});
This event handler is triggering before clicking on the download file, and the uploaded files are disappearing.
Can you please help me with this?
Thanks.
Hello Monica,
The
survey.onDownloadFile
event is raised when a File Upload question renders a file preview. It is not fired when a user clicks a link to download a file.I would recommend that you refer to the following demo to learn how to implement the
onDownloadFile
event to correctly download files from a server: Retrieve Uploaded Files for Preview Using File Names. The code may vary depending on your storage server configuration. In reference to the demo code, pay special attention that the actual file URL/link is stored within theoptions.content
property.survey.onDownloadFile.add((_, options) => { fetch("https://api.surveyjs.io/private/Surveys/getTempFile?name=" + options.content) .then((response) => response.blob()) .then((blob) => { const file = new File([blob], options.fileValue.name, { type: options.fileValue.type }); const reader = new FileReader(); reader.onload = (e) => { options.callback("success", e.target.result); }; reader.readAsDataURL(file); }) .catch((error) => { console.error("Error: ", error); options.callback("error"); }); });
Please let me know if it helps.
P.S. Let me note that technical support are available for commercial users only. If you wish to receive technical Help Desk support, consider purchasing a developer license. On a free plan, please feel free to submit a GitHub issue at https://github.com/surveyjs/survey-library/issues.
Thank you