Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tito 314 posts 623 karma points
    Nov 30, 2022 @ 10:46
    Tito
    0

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

    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.

  • Mitch 44 posts 159 karma points
    Apr 13, 2023 @ 10:51
    Mitch
    0

    Hi @Tito. Did you get anywhere with this?

  • Tito 314 posts 623 karma points
    Apr 14, 2023 @ 07:18
    Tito
    100

    Yes, i made it. Basically you have to save each IFormFile uploaded to a folder and store the paths as an array.

    You can check my code, the result variable is what you have to store using forms api. First you check the type of the propertyValue and if IFormFile save it and get the path. Its very important to set the path right. There is a bug, if the path its not set right, when deleting the record it deletes the entire media folder. I reported the bug but i dont know if it is solved because i didnt get an answer :(

      var result = new List<object>();
    
                var tipo = propertyValue.GetType();
                if (tipo == typeof(StringValues))
                {
                    var propertyValueSTR = propertyValue.ToString();
                    if (propertyValueSTR == null) return result;
                    result.Add(propertyValueSTR);
                }
                else if (tipo == typeof(List<IFormFile>))
                {
                    var files = propertyValue as List<IFormFile>;
                    if (files != null)
                    {
                        foreach (var file in files)
                        {
                            if (file != null)
                            {
                                string uploadsFolder = string.Format(@"/media/forms/upload/form_{0}/{1}/{2}/", formId, recordKey, fieldKey);
                                var uploadsFolderAbs = this._helper.MapPath(uploadsFolder);
                                if (!Directory.Exists(uploadsFolderAbs))
                                    Directory.CreateDirectory(uploadsFolderAbs);
                                var filePath = Path.Combine(uploadsFolder, file.FileName);
                                var filePathAbs = this._helper.MapPath(filePath);
                                if (System.IO.File.Exists(filePathAbs))
                                    System.IO.File.Delete(filePathAbs);
                                using (Stream fileStream = new FileStream(filePathAbs, FileMode.Create))
                                {
                                    file.CopyTo(fileStream);
                                }
                                result.Add(filePath);
                            }
    
                        }
                    }
    
                }
    
Please Sign in or register to post replies

Write your reply to:

Draft