How don't save upload file on disk

How don't save upload file on disk

SuiteProGSuiteProG Posts: 14Questions: 2Answers: 0
edited November 21 in Free community support

Hello,

I use DataTable editor with c#.NET library.

One on my fields it's "upload" type.
I don't want save the file in disk, I want directly save content in my DB.
What's method for this ?

This code save content on my DB and on my disk :

  var response = new Editor(db, "users")
                    .Model<UploadModel>()
                            .TryCatch(false)
                    .Field(new Field("image")
                        .SetFormatter(Format.IfEmpty(null))
                        .Upload(new Upload(request.PhysicalApplicationPath + @"uploads\__ID____EXTN__")
                            .Db("files", "id", new Dictionary<string, object>
                            {
                                {"web_path", Upload.DbType.WebPath},
                                {"system_path", Upload.DbType.SystemPath},
                                {"filename", Upload.DbType.FileName},
                                {"filesize", Upload.DbType.FileSize},
                                {"content", Upload.DbType.ContentBinary},
                            })
                            .DbClean(data =>
                            {
                                foreach (var row in data)
                                {
                                    // Do something;
                                }
                                return true;
                            })
                            .Validator(Validation.FileSize(500000, "Max file size is 500K."))
                            .Validator(Validation.FileExtensions( new [] {"jpg", "png", "gif"}, "Please upload an image."))
                        )
                    )
                    .Process(request)
                    .Data();

I try Upload(null) by don't work.

Thanks,

Answers

  • allanallan Posts: 48,452Questions: 1Answers: 7,015 Site admin

    Hi,

    Don't pass anything to the new Upload() constructor. That should mean that it doesn't write the file to the file system itself.

    Allan

  • SuiteProGSuiteProG Posts: 14Questions: 2Answers: 0

    Hi Allan,

    Thanks for your reply.

    When I use new Upload(), I have a NullReferenceException with this stacktrace :

           à CallSite.Target(Closure , CallSite , Func`3 , HttpPostedFile , Object )
           à System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
           à DataTables.Upload._actionExec(Object id, HttpPostedFile upload)
           à System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
           à DataTables.Upload.Exec(Editor editor)
           à DataTables.Editor._Upload(DtRequest data)
           à DataTables.Editor._Process(DtRequest data)
           à DataTables.Editor.Process(DtRequest data)
           à DataTables.Editor.Process(NameValueCollection data)
           à DataTables.Editor.Process(HttpRequest request)
           à Editor_NET_Framework_Demo.Controllers.UploadController.Staff() dans C:\Users\Charly\Documents\Preuves de concepts\Editor-NETFramework-1.8.0\Editor-NETFramework-1.8.0\Editor NET Framework Demo\Controllers\UploadController.cs:ligne 31
           à lambda_method(Closure , Object , Object[] )
           à System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_1.<GetExecutor>b__3(Object instance, Object[] methodParameters)
           à System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
           à System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
    

    My new code :

     var response = new Editor(db, "users")
                        .Model<UploadModel>()
                                .TryCatch(false)
                        .Field(new Field("image")
                            .SetFormatter(Format.IfEmpty(null))
                                //.Upload(new Upload(request.PhysicalApplicationPath + @"uploads\__ID____EXTN__")
                                .Upload(new Upload()
                                .Db("files", "id", new Dictionary<string, object>
                                {
                                    //{"web_path", Upload.DbType.WebPath},
                                    //{"system_path", Upload.DbType.SystemPath},
                                    {"filename", Upload.DbType.FileName},
                                    {"filesize", Upload.DbType.FileSize},
                                    {"file_content", Upload.DbType.ContentBinary},
                                })
                                .DbClean(data =>
                                {
                                    foreach (var row in data)
                                    {
                                        // Do something;
                                    }
                                    return true;
                                })
                                .Validator(Validation.FileSize(500000, "Max file size is 500K."))
                                .Validator(Validation.FileExtensions(new[] { "jpg", "png", "gif" }, "Please upload an image."))
                            )
                        )
                        .Process(request)
                        .Data();
    
  • SuiteProGSuiteProG Posts: 14Questions: 2Answers: 0

    Hi @allan,

    I always have a NullReferenceException with no parameters Upload constructor.

    Cheers

  • allanallan Posts: 48,452Questions: 1Answers: 7,015 Site admin

    Sorry about that. A bug in the library if there is no string action it assumes there will be a function action. Which isn't the case here. I've committed a fix and you can either build the dll from that repo or I can send you an update if you prefer.

    Regards,
    Allan

  • SuiteProGSuiteProG Posts: 14Questions: 2Answers: 0

    Hi @allan,

    Thanks for your reply.
    If you can send an update to me, I prefer :)

    Cheers

  • SuiteProGSuiteProG Posts: 14Questions: 2Answers: 0

    Hi @allan

    Can I have update dll ?

    Cheers

  • allanallan Posts: 48,452Questions: 1Answers: 7,015 Site admin

    Oops - sorry! I completely lost track of this thread. I've sent you a PM with a link for the updated dll.

    Allan

Sign In or Register to comment.