vs.net 2017 MVC use upload

vs.net 2017 MVC use upload

tdx369tdx369 Posts: 5Questions: 1Answers: 0

I want use Editor upload function in vs.net 2017 MVC . Code as below:

[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public ActionResult HData()
{
var formData = HttpContext.Request.Form;

        string connString = System.Configuration.ConfigurationManager.ConnectionStrings["Entities"].ToString();
        string username = GetRealName();
        using (var Ddb = new Database("sqlserver", connString))
        {
            var response = new Editor(Ddb, "HDevices")
               .Model<HDeviceItem>().TryCatch(false)                
                  .Field(new Field("UpdateDate")                     
                  .Set(Field.SetType.Edit)
                  .SetValue(DateTime.Now.ToString("yyyy-MM-dd"))
                  .GetFormatter(Format.DateSqlToFormat(Format.DATE_ISO_8601))
                  .SetFormatter(Format.DateFormatToSql(Format.DATE_ISO_8601)))
                   .Field(new Field("Hfile")
                    .SetFormatter(Format.IfEmpty(null))
                    .Upload(new Upload(HttpContext.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}
                        })
                        .DbClean(data =>
                        {
                            foreach (var row in data)
                            {
                                // Do something;
                            }
                            return true;
                        })
                        .Validator(Validation.FileSize(5000000, "Max file size is 5000K."))
                        .Validator(Validation.FileExtensions(new[] { "jpg", "png", "gif", "pdf" }, "Please upload an image."))
                    )
                )
                .Process(formData)
                .Data();

            return Json(response, JsonRequestBehavior.AllowGet);
        }
    }

When the debug begin ,it report wrong like below

System.Exception
HResult=0x80131500
Message=File upload requires that 'Process' be called with an HttpRequest or UnvalidatedRequestValues object
Source=DataTables

How can I revise code?

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    edited July 2018

    Change:

    var formData = HttpContext.Request.Form;

    To be:

    var formData = HttpContext.Request;
    

    And that should do it. That allows Editor to access the file upload information from the request as well as the form data.

    Allan

  • tdx369tdx369 Posts: 5Questions: 1Answers: 0

    change to below is ok :)
    var formData = HttpContext.ApplicationInstance.Context.Request

    thanks too.

This discussion has been closed.