Need .Net Core Upload Working Example w/Unique Id Generation

Need .Net Core Upload Working Example w/Unique Id Generation

TyreekusTyreekus Posts: 2Questions: 1Answers: 0

I've been struggling to get the Editor Upload to work for a simple form upload. The examples are designed for the .Net Framework and some of the functions don't exist in .Net Core.

I'm trying to create a new File and then save that FileId to a Linked Table.

If anyone has a working example to share I would be very appreciative.

This is an example of my controller that is not working.

      [Route("api/images-post/{recipeId?}")]
                [HttpPost]
                public JsonResult ImagesPost(string recipeId)
                {
                    var dbConn = Environment.GetEnvironmentVariable("DBCONNECTION");
                    var dbType = Environment.GetEnvironmentVariable("DBTYPE");        
        
                    using (var db = new Database(dbType, dbConn))
                    {
                        var idx = Guid.NewGuid().ToString();
                        var fileExtn = Path.GetExtension(Request.Form.Files[0].FileName);
                        var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads", idx + fileExtn);
                        var newFile = Request.Form.Files[0];
        
                        var response = new Editor(db, "RecipeFiles")
                            .Debug(true)
                            .TryCatch(false)
                            .Model<DtRecipeFiles>("RecipeFiles")
        
                            .Field(new Field("RecipeFiles.FileId")
                                .Upload(
                                new Upload((file, id) =>
                                {                            
                                    if (id == null || id == "")
                                    {
                                        id = idx;
                                    }
        
                                    using (var fs = System.IO.File.Create(filePath))
                                    {
                                        file.CopyTo(fs);
                                    }
        
                                    return id;
                                })
                                .Db("Files", "Id", new Dictionary<string, object>
                                {
                                    {"Id",idx },
                                    {"WebPath",Path.DirectorySeparatorChar + Path.Combine("uploads", idx + fileExtn) },
                                    {"SystemPath", filePath},
                                    {"FileName",Request.Form.Files[0].FileName },
                                    {"FileSize",Request.Form.Files[0].Length},
                                    {"FileExtension", fileExtn }
                                })))
        
        
                            .Field(new Field("RecipeFiles.RecipeId")
                                .SetValue(recipeId))
        
                            .Field(new Field("RecipeFiles.Id")
                                .SetValue(Guid.NewGuid().ToString())
                                .Set(Field.SetType.Create))
        
                            .LeftJoin("Files", "Files.Id", "=", "RecipeFiles.FileId")
                            .Where("RecipeFiles.RecipeId", recipeId)
                            .Process(Request)
                            .Data();
        
                        return Json(response);
                    }
        
                }

Answers

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin

    Hi,

    The .NET Core download includes an upload example which I believe operates the way you are looking for (without needing to specify your own upload action - just give it a path where to save the file, like in the example).

    Allan

This discussion has been closed.