Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving Umbraco Forms records programatically with file uploads gets "Object must implement IConvertible" error #930

Closed
teeto opened this issue Nov 30, 2022 · 5 comments

Comments

@teeto
Copy link

teeto commented Nov 30, 2022

Hi, i am using Umbraco Forms 10.2.0 in Umbraco 10.3.2

I have an api controller that receives IFormCollection values and want to save it to an umbraco form. I have succesfully made it using IRecordFieldStorage, but the problem is when i have a field of type file upload.

I am using axios to post the values, set the header content type to "multipart/form-data" and send the data as FormData so i can received the files.

What i am receiving is a value of type "FormFile" and when i try to save it to the record using InsertRecordField from IRecordFieldStorage i get this error: "Object must implement IConvertible".

Which format is expecting the record field storage to have a file upload?

I cant get the documentation right for that situation. Do i have to manage the file saving and pass the path? i guess the Umbraco Forms api should do that task so i can export the form records with proper file uploads.

@AndyButland
Copy link

Your question likely relates to an issue we still have to look into, regarding supporting file uploads via the headless/AJAX API, which is similar to what you are looking to do.

Hopefully once we get chance to look at that and release it, you'll be able to use that instead of your own implementation.

In the meantime I'd suggest looking at what's stored for upload fields for forms submitted via a normal HTML form - which you'll see is e.g. /media/forms/upload/form_3623b232-9296-4bf0-b16c-57801dc4f296/a7843c61-f653-40c2-8517-c01a4383ad5a/hp.jpg.

So you could look at saving the file into the media system, getting the path, and saving that. But I can't really advise what's best here - would have to dig more to find out if you can use what Forms already does, which I think you may be by-passing by going direct to IRecordFieldStorage, Will keep a note to share more here when we get to looking at this for our own API.

@teeto
Copy link
Author

teeto commented Nov 30, 2022

Thanks Andy, in the meantime i have been able to achieve the result.
I have stored the file in that path, which i guess is: /media/forms/upload/form_{formId}/{record_unique_id}/{field_id}/file.pdf
And that path is what i store as the value. If multiple file, i store an array of paths.
It is working, and, if i delete the records, the files get deteted as well.
One thing that it is not working and in old versions works, is the option to export all the attachments as a zip file, i cant see the option. Is not available in new versions or is it that the form thinks it has no attachments becouse of what i am storing?
The ideal way would be to be able to pass an array of FormFile objects and let the umbraco forms api do the magic.

@AndyButland
Copy link

We have some logic in forms that removes that option if you don't have the built-in upload field in your form, or you do, but there are no files found that are uploaded in the media file system.

@teeto
Copy link
Author

teeto commented Dec 2, 2022

@AndyButland i have found a very important bug.
If no upload is selected what is coming is an empty object of FileList in javascript.
As no upload is found, it is storing the string "[object FileList]" in the FileUpload field, i have fixed it storing null instead.
The bug is that if storing "[object FileList]" in the fileupload field, if you view the record in Umbraco Forms entries, the field is blank and if you delete the record, i dont know why the entire "media" folder is deleted.
So i guess the bug is that if an incorrect value is stored in the FileUpload field, deleting the record, may cause delete the entire media folder. Please could you check it?

@AndyButland
Copy link

We'll close this issue now given the related feature request about file uploads as been actioned, as has the bug report which is also handled via another issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants