.net upload files
.net upload files
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
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
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:
controller:
javascript:
Your upload is inside an
Mjoin
so you would need to usename: "Attachments[].AttachmentID",
and alsotype: '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
ahh, I see. I would just use a regular Left Join if I am testing the single file upload.