Upload file data into DB

Upload file data into DB

joshlevine102joshlevine102 Posts: 44Questions: 16Answers: 2

Hello. Trying to upload a file just into a column in the DB, not a location on the server. They don't let us save files on the web server, unfortunately. Using the C#/.NET Editor code. It looks like I want to use Upload.DbType.ContentBinary to save the file contents into a MS SQL Server column of type varbinary. I seem to be getting a Null Reference exception from this line in Upload.cs.

        private dynamic _actionExec(dynamic id, HttpPostedFile upload)
        {
            if (_actionStr == null)
            {
                // Custom function
                return _actionFn(upload, id);     // Seem to be getting a null reference here, _actionFn is null.
            }

Is the problem that I'm not defining an Action for the upload and am not setting a file location in the Upload constructor? This is what my lines for the Upload Field in my controller look like (Thanks):

                    .Field(new Field("bis401_sites.file1")
                    .Upload(new Upload()
                    .Db("bis401_sites_files", "id", new Dictionary<string, object>
                    {
                        {"fileName", Upload.DbType.FileName},
                        {"fileSize", Upload.DbType.FileSize},
                        {"fileContent", Upload.DbType.ContentBinary}
                    }))
                    .SetFormatter(Format.NullEmpty()))

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin

    Thanks for your message. I'll try to reproduce this locally and see what is going wrong.

    Allan

  • joshlevine102joshlevine102 Posts: 44Questions: 16Answers: 2

    Allan, thanks for taking a look. I have a workaround now, just by adding a bit to that Upload.cs to check if the actionFn is null.

  • joshlevine102joshlevine102 Posts: 44Questions: 16Answers: 2

    Just to follow up, I upgraded to the 1.7.3 version of Editor, and this null ref exception seems to still be happening, at the same line mentioned above (for me, line 473 in Upload.cs, _actionFn seems to be null). I'm only putting the file binary data in the DB, not in a directory on the server (so I'm using the no-arg constructor of Upload), my Field setup is similar to the above, but I switched to using uploadMany. I had modified my own copy of Upload.cs earlier to look like the following. Not sure if this is the right idea, but it seemed to make it work.

            private dynamic _actionExec(dynamic id, HttpPostedFile upload)
            {
                if (_actionStr == null)
                {
                    // Custom function
                    if (_actionFn != null)
                    {
                        return _actionFn(upload, id);
                    }
                    else
                    {
                        if (id != null)
                        {
                            return id;
                        }
                    }
                }
    

    If it looks like I'm messing up something, please let me know. Thanks.

  • joshlevine102joshlevine102 Posts: 44Questions: 16Answers: 2
    Answer ✓

    Ah, it seems like if I leave Upload.cs alone, and make this change to put this anonymous function in the call to the Upload constructor, it also works.

    .Upload(new Upload((file,id) => { return id; }) 
    
This discussion has been closed.