.net upload files

.net upload files

montoyammontoyam Posts: 568Questions: 136Answers: 5

I am having a heck of a time getting the .net version of fileupload working. Eventually I am wanting to enable uploadMany but figured I would start with the 'easier' single upload. But, I can't even get that going.

I have three tables:
Submissions: SubmissionID
Attachments: AttachmentID, FileName, etc.
SubmissionAttachments: SubmissionAttachmentID, SubmissionID, AttachmentID

I am currently getting the error: Unknown upload field name submitted

here is my controller:

/*
    * Controller for DB table Submissions
    * Created by http://editor.datatables.net/generator
    */
using System;
using System.Collections.Generic;
using System.Net.Http.Formatting;
using System.Web;
using System.Web.Http;
using DataTables;
using EditorGenerator.Models;
            
namespace EditorGenerator.Controllers
{
    public class SubmissionsController : ApiController
    {
        [Route("api/Submissions")]
        [HttpGet]
        [HttpPost]
        public IHttpActionResult Submissions()
        {
            var request = HttpContext.Current.Request;
            var settings = Properties.Settings.Default;
            
            using (var db = new Database(settings.DbType, settings.DbConnection))
            {
                var response = new Editor(db, "Submissions", "SubmissionID")
                    .Model<SubmissionsModel>()
                    .MJoin(new MJoin("Attachments")
                    .Link("Submissions.SubmissionID", "SubmissionAttachments.SubmissionID")
                    .Link("Attachments.AttachmentID", "SubmissionAttachments.AttachmentID")
                    .Field(
                    new Field("SubmissionAttachments.AttachmentID")
                    .Upload(
                        //upload(Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/upload/__ID__.__EXTN__')
                    new Upload(request.PhysicalApplicationPath + @"uploads\__ID____EXTN__")
                    .Db("Attachments", "AttachmentID", new Dictionary<string, object>
                    {
                        {"FileName", Upload.DbType.FileName},
                        {"FileSize", Upload.DbType.FileSize},
                        {"WebPath", Upload.DbType.WebPath},
                        {"SystemPath", Upload.DbType.SystemPath}
                    })
                    .Validator(Validation.FileSize(50000000, "Max file size is 500000K."))
                    //.Validator(Validation.FileExtensions(new[] { "jpg", "png", "gif", "html", "htm" }, "Please upload an image or html file."))
                    )
                    .SetFormatter(Format.NullEmpty())
                    )
                    )
                     .Process(request)
                    .Data();

                return Json(response);
            }
        }
    }
}

and my javascript


/* * Editor client script for DB table Submissions * Created by http://editor.datatables.net/generator */ (function($){ $(document).ready(function() { var editor = new $.fn.dataTable.Editor( { ajax: '/api/Submissions', table: '#Submissions', fields: [ { "label": "EmployeeName:", "name": "EmployeeName" }, { label: 'Document(s):', name: "Submissions.AttachmentID", //type: "uploadMany", type: "upload", dragDropText: 'Drag and Drop to Upload', uploadText: 'Choose Document ...', noFileText: 'No Documents', processingText: 'Processing ...', fileReadText: 'Uploading Document' } ] } ); var table = $('#Submissions').DataTable( { dom: 'Bfrtip', ajax: '/api/Submissions', columns: [ { "data": "EmployeeName" } ], select: true, lengthChange: false, buttons: [ { extend: 'create', editor: editor }, { extend: 'edit', editor: editor }, { extend: 'remove', editor: editor } ] } ); } ); }(jQuery));

Again, my final goad is to have 'UploadMany'. Any help would be greatfuly appreciated. I have found many php examples, and I thought I was able to copy/paste a couple .net examples from forum posts, but I can't get anything to work.

This question has an accepted answers - jump to answer

Answers

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    So I found another post with code and I get a different message: File uploaded to a field that does not have upload options configured

    /*
        * Controller for DB table Submissions
        * Created by http://editor.datatables.net/generator
        */
    using System;
    using System.Collections.Generic;
    using System.Net.Http.Formatting;
    using System.Web;
    using System.Web.Http;
    using DataTables;
    using EditorGenerator.Models;
                
    namespace EditorGenerator.Controllers
    {
        public class SubmissionsController : ApiController
        {
            [Route("api/Submissions")]
            [HttpGet]
            [HttpPost]
            public IHttpActionResult Submissions()
            {
                var request = HttpContext.Current.Request;
                var settings = Properties.Settings.Default;
                
                using (var db = new Database(settings.DbType, settings.DbConnection))
                {
                    var response = new Editor(db, "Submissions", "SubmissionID")
                        .Model<SubmissionsModel>("Submissions")
                        .MJoin(new MJoin("Attachments")
                            .Link("Submissions.SubmissionID", "SubmissionAttachments.SubmissionID")
                            .Link("Attachments.AttachmentID", "SubmissionAttachments.AttachmentID")
                            .Field(
                            new Field("AttachmentID")
                                .Upload(
                               new Upload(request.PhysicalApplicationPath + @"uploads\__ID____EXTN__")
                                    .Db("Attachments", "AttachmentID", new Dictionary<string, object>
                                    {
                                        {"FileName", Upload.DbType.FileName},
                                        {"WebPath", Upload.DbType.WebPath},
                                        {"SystemPath", Upload.DbType.SystemPath},
                                    })
                                    //.Validator(Validation.FileSize(500000, "Max file size is 500K."))
                                    //.Validator(Validation.FileExtensions(new[] { "jpg", "png", "gif" }, "Please upload an image."))
                                    )
                            )
                        )
                         .Process(request)
                        .Data();
    
                    return Json(response);
                }
            }
        }
    }
    
  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    So I realized my javascript was referencing Submissions.AttachmentID, not Attachments.AttachmentID. I have changed it. But now get the error: Unknown upload field name submitted

    model:

    /*
     * Model for DB table Submissions
     * Created by http://editor.datatables.net/generator
     */
    using System;
    
    namespace EditorGenerator.Models
    {
        public class SubmissionsModel
        {
            public string EmployeeName { get; set; }
            public int SubmissionID { get; set; }
        }
    
        public class AttachmentsModel
        {
            public int AttachmentID { get; set; }
            public string FileName { get; set; }
            public int FileSize { get; set; }
            public string WebPath { get; set; }
            public string SystemPath { get; set; }
        }
    
        public class SubAttachModel
        {
            public int SubmissionID { get; set; }
            public int AttachmentID { get; set; }
        }
    }
    

    controller:

    /*
        * Controller for DB table Submissions
        * Created by http://editor.datatables.net/generator
        */
    using System;
    using System.Collections.Generic;
    using System.Net.Http.Formatting;
    using System.Web;
    using System.Web.Http;
    using DataTables;
    using EditorGenerator.Models;
                
    namespace EditorGenerator.Controllers
    {
        public class SubmissionsController : ApiController
        {
            [Route("api/Submissions")]
            [HttpGet]
            [HttpPost]
            public IHttpActionResult Submissions()
            {
                var request = HttpContext.Current.Request;
                var settings = Properties.Settings.Default;
                
                using (var db = new Database(settings.DbType, settings.DbConnection))
                {
                    var response = new Editor(db, "Submissions", "SubmissionID")
                        .Model<SubmissionsModel>("Submissions")
                        .MJoin(new MJoin("Attachments")
                            .Link("Submissions.SubmissionID", "SubmissionAttachments.SubmissionID")
                            .Link("Attachments.AttachmentID", "SubmissionAttachments.AttachmentID")
                            .Field(
                            new Field("AttachmentID")
                                .Upload(
                               new Upload(request.PhysicalApplicationPath + @"uploads\__ID____EXTN__")
                                    .Db("Attachments", "AttachmentID", new Dictionary<string, object>
                                    {
                                        {"FileName", Upload.DbType.FileName},
                                        {"WebPath", Upload.DbType.WebPath},
                                        {"SystemPath", Upload.DbType.SystemPath},
                                    })
                                    //.Validator(Validation.FileSize(500000, "Max file size is 500K."))
                                    //.Validator(Validation.FileExtensions(new[] { "jpg", "png", "gif" }, "Please upload an image."))
                                    )
                            )
                        )
                         .Process(request)
                        .Data();
    
                    return Json(response);
                }
            }
        }
    }
    

    javascript:


    /* * Editor client script for DB table Submissions * Created by http://editor.datatables.net/generator */ (function($){ $(document).ready(function() { var editor = new $.fn.dataTable.Editor( { ajax: '/api/Submissions', table: '#Submissions', fields: [ { "label": "EmployeeName:", "name": "Submissions.EmployeeName" }, { label: 'Document(s):', name: "Attachments.AttachmentID", //type: "uploadMany", type: "upload", dragDropText: 'Drag and Drop to Upload', uploadText: 'Choose Document ...', noFileText: 'No Documents', processingText: 'Processing ...', fileReadText: 'Uploading Document' } ] } ); var table = $('#Submissions').DataTable( { dom: 'Bfrtip', ajax: '/api/Submissions', columns: [ { "data": "Submissions.EmployeeName" } ], select: true, lengthChange: false, buttons: [ { extend: 'create', editor: editor }, { extend: 'edit', editor: editor }, { extend: 'remove', editor: editor } ] } ); } ); }(jQuery));
  • allanallan Posts: 61,663Questions: 1Answers: 10,095 Site admin
    Answer ✓

    Your upload is inside an Mjoin so you would need to use name: "Attachments[].AttachmentID", and also type: 'uploadMany' (since its an Mjoin).

    If you need an Mjoin, then this is the example to follow (or rather, its C# equivalent from the .NET download).

    Allan

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    ahh, I see. I would just use a regular Left Join if I am testing the single file upload.

This discussion has been closed.