.net editor and upload

.net editor and upload

memamema Posts: 8Questions: 2Answers: 0

Hi

I am trying to upload an image using the upload function i the datables editor and the .net backend.

I would like to use custom action and save the file in a custom folder on the webserver and just save the path in a field in the table the editor is using.

But whenever I try to add the

.Field(new Field("Billede")
    .Upload(new Upload((file, id) => {
        file.SaveAs(context.Request.PhysicalApplicationPath+@"Files\Images\Vouchers\"+file.FileName);
        return @"Files\Images\Vouchers\" + file.FileName;
    }))
)

I get an error saying "A value must not be null. Parametername: key" when loading the datatable?? If I remove the Upload from the Field, it works, but I cannot upload as no field is defined as upload.

Why is that? Am I approaching this wrong?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin

    Hi,

    The code looks good - I would expect that to work. Could you show me the model you are using? I'm wondering if the field (probably the Billede field) is nullable or not?

    Does the error occur before you've uploaded any files - i.e. immediately as soon as you add the above and the database contains null values for Billede (at least that is what I assume to be the case!)?

    Thanks,
    Allan

  • memamema Posts: 8Questions: 2Answers: 0
    edited November 2015

    In my model the 'Billede' (which is danish for picture) property is a string, so it should be nullable :-)

    I have tried with both with and without data in the 'Billede' field in the database. Since I played with it most of last night, I am to tired to make a sample projekt right now.

    I might do it tomorrow, if it would help.

  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin

    It would certainly be interesting if that is possible. If not, I'll try to recreate the issue locally tomorrow myself.

    Could you confirm if this happens on page load (i.e the Ajax request to get the data for the table) before any files have been uploaded, after a file has been uploaded and the page it reloaded or immediately after a file has been uploaded?

    One other thing - are you using the 1.5.3 libraries for .NET?

    Thanks,
    Allan

  • memamema Posts: 8Questions: 2Answers: 0
    edited December 2015

    Hi Allan,

    Did not get a chance to make a POC project. The error occurs in the ajax load when the datatable tries to get the data.
    Below is the actual part of the handler, that the table calls.

    using (var db = new Database("sqlserver", ApplicationSettings.Instance.ConnectionString))
    {
        var response = new Editor(db, "VoucherType", "Id")
            .Model<VoucherType>()
            .Field(new Field("Billede")
            .Upload(new Upload((file, id) =>
            {
                file.SaveAs(context.Request.PhysicalApplicationPath + @"Files\Images\Vouchers\" + file.FileName);
                return @"Files\Images\Vouchers\" + file.FileName;
            } ))
            )
            .Field(new Field("StartDate")
                .Validator(Validation.DateFormat(
                    Format.DATE_ISO_8601,
                    new ValidationOpts { Message = "Benyt venligst formatet yyyy-mm-dd" }
                    ))
                .GetFormatter(Format.DateSqlToFormat(Format.DATE_ISO_8601))
                .SetFormatter(Format.DateFormatToSql(Format.DATE_ISO_8601))
            )
            .Field(new Field("EndDate")
                .Validator(Validation.DateFormat(
                    Format.DATE_ISO_8601,
                    new ValidationOpts { Message = "Benyt venligst formatet yyyy-mm-dd", Empty = true }
                    ))
                .SetFormatter(Format.NullEmpty())
                .GetFormatter(Format.DateSqlToFormat(Format.DATE_ISO_8601))
            )
            .Field(new Field("Hastighed")
                .SetFormatter((val, data) => ((Dictionary<string, object>)val).GetValueOrDefault("0").ToString())
            )
        .Process(context.Request)
        .Data();
        context.Response.ContentType = "application/json";
        context.Response.Write(response.SerializeObjectToJSON());
        context.Response.StatusCode = 200;
        return;
    }
    

    My VoucherType is defined as following:

    public class VoucherType
    {
        public int Id { get; set; }
        public string Navn { get; set; }
        public string Billede { get; set; }
        public string Title { get; set; }
        public string Beskrivelse { get; set; }
        public string StartDate { get; set; }
        public string EndDate { get; set; }
        public string Active{ get; set; }
        public string Link { get; set; }
        public string OnlyNewUsers { get; set; }
        public string Hastighed { get; set; }
    }
    

    And yes, I am using version 1.5.3 og the .net library

  • memamema Posts: 8Questions: 2Answers: 0

    Ohh. I forgot to add the table as well.

    CREATE TABLE [dbo].VoucherType(
        [Id] [int] IDENTITY(1,1) NOT NULL,
        [Navn] [nvarchar](50) NOT NULL,
        [Billede] [nvarchar](max) NULL,
        [Title] [nvarchar](max) NULL,
        [Beskrivelse] [nvarchar](max) NULL,
        [Startdate] [date] NOT NULL,
        [Enddate] [date] NULL,
        [Active] [bit] NOT NULL,
        [Link] [nvarchar](255) NULL,
        [OnlyNewUsers] [bit] NULL,
        [Hastighed] [nvarchar](max) NULL,
        CONSTRAINT [PK_VoucherType] PRIMARY KEY CLUSTERED 
    (
        [Id] ASC
    )
    
  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin

    Hi,

    Sorry I haven't had a chance to look at this in detail yet. I've not forgotten about it and will get back to you as soon as possible!

    Regards,
    Allan

  • memamema Posts: 8Questions: 2Answers: 0

    Hi Allan.

    No worries... I haven't supplied you with a working sample project either. And I know you have other obligations as well. I've been quit busy with other projects, too.

    Mikkel

  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    Answer ✓

    HI Mikkel,

    Thanks for your patience with this. It is without question a bug in the Editor libraries that is triggered when there is no Db method specified for the file upload. I've committed the fix locally and it will be part of Editor 1.5.4 which I expect to release soon (possibly tomorrow now).

    If you want to fix it immediately, in the Editor.cs file there is a method called _FileDataFields - where the var table = upload.Table(); is defined it should be:

                    var table = upload.Table();
    
                    if (table == null)
                    {
                        continue;
                    }
    

    i.e. check if table is null or not. It is with the configuration you have and it is failing without that check.

    Regards,
    Allan

  • memamema Posts: 8Questions: 2Answers: 0

    Great thnx. I will look forward to 1.5.4 and update the code when it is released.

This discussion has been closed.