Forum



22:33

21/06/2012

What I'm looking for is a working example of jqGrid file upload from the add/edit form in an Microsoft MVC C# application.
This is what I've tried, (among others) and I always end up with a null file passed back to the controller.
MVC3 app using :
AjaxFileUpload.js
jQuery-1.7.2
jqGrid-1.4.4
I apologize for the formatting. If there's a way to retain indentations please tell me. I'll resubmit.
The AfterSubmit for the "Add function" is called but kicks out with ret undefined.
Occuring at line 7532:
I'm not sure what the response should be from the CreateAttachmentRecord, which is used in the data argument. . I'm sending:{"id":"fileToUpload", "success"=true}. I suspect the data field is wrong, but don't know what I should have there.
JQUERY CODE:
$(document).ready(function () {
'use strict';
jQuery("#table").jqGrid({
datatype: "local",
height: 250,
editurl: "Home/CreateAttachmentRecord",
colNames:['fileToUpload','Title','Description','Control Class','Added By'],
colModel:[
{
name: 'fileToUpload',
index: 'Attachment',
align: 'left',
editable: true,
edittype: 'file',
editoptions: {
enctype: "multipart/form-data"
},
width: 210,
search: false,
editrules:{required:true}
},
{name:'Title',index:'Title', width:50, editrules:{required:false}, editable:true, sorttype:"string"},
{name:'Description',index:'Description', width:100, editrules:{ required:true}, editable:true,sorttype:"string"},
{name:'ControlClass',index:'ControlClass', width:10, editrules:{ required:true}, editable:true, sorttype:"string"},
{name:'AddedBy',index:'AddedBy', width:50, editrules:{required:true}, editable:true,sorttype:"string"},
],
multiselect: true,
caption: "Attachments for: [CR Name Here]",
rowNum:10,
rowList:[10,20,30],
pager: '#pager',
sortname: 'Filename',
viewrecords: true,
sortorder: "desc",
});
var mydata = [
@foreach (var attach in Model) {
@Html.Raw("{");
@Html.Raw( string.Format(
"fileToUpload: "{0}",Title: "{1}",Description: "{2}",ControlClass: "{3}",AddedBy: "{4}" ",
attach.Filename,
attach.Title,
attach.Description,
attach.ControlClass,
attach.AddedByName
)
)
@Html.Raw("},n");
}
];
for(var i=0;i<mydata.length;i++) {
jQuery('#table').jqGrid('addRowData',i+1,mydata[i]);
};
jQuery("#table").jqGrid('navGrid','#pager',
{del:true,add:true,edit:true},
{ jqModal:true,closeAfterEdit: true,recreateForm:true,onInitializeForm : function(formid){ //editoptions
$(formid).attr('method','POST');
$(formid).attr('action','');
$(formid).attr('enctype','multipart/form-data');
},
afterSubmit : function(response, postdata){
$.ajaxFileUpload({
url: 'Home/UploadFile',
secureuri:false,
fileElementId:'fileToUpload',
dataType: 'JSON',
success: function (data, status) {
alert("Upload Complete.");
}
});
}
},
{jqModal:true,closeAfterAdd: true,recreateForm:true,onInitializeForm : function(formid){
$(formid).attr('method','POST');
$(formid).attr('action','');
$(formid).attr('enctype','multipart/form-data');
},
afterSubmit : function(response, postdata){
$.ajaxFileUpload({
url: 'Home/UploadFile',
secureuri:false,
fileElementId:'fileToUpload',
dataType: 'JSON',
success: function (data, status) {
alert("Upload Complete.");
}
});
}
}
);
CONTROLLER CODE:
HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file)
{
string foldername = "~/App_data/" + "5397″;
if (!Directory.Exists(Server.MapPath(foldername)))
{
Directory.CreateDirectory(Server.MapPath(foldername));
}
if (file != null && file.ContentLength > 0)
{
// extract only the fielname
var fileName = Path.GetFileName(file.FileName);
// store the file inside ~/App_Data/uploads folder
var path = Path.Combine(Server.MapPath(foldername), fileName);
file.SaveAs(path);
}
return Json(new
{
success = true
}, JsonRequestBehavior.AllowGet);
}
// This action handles the form POST and the upload
[HttpPost]
public ActionResult CreateAttachmentRecord(string filename,string title, string description, string controlClass, string addedby)
{
AttachmentModel attach = new AttachmentModel();
attach.Filename = filename;
attach.Description = description;
attach.ControlClass = controlClass;
attach.AddedByName = "the current user";
attach.Title = title;
attachments.Add(attach);
return Json(new
{
id = "fileToUpload",
success = true
}, JsonRequestBehavior.AllowGet);
}
15:29

Moderators
30/10/2007

Hello,
Sorry, but my ASP NET MVC knowledge is very limited.
Basically - should I think that you want to submit the file contents again with record add/edit?
The afterSubmit indicates this behaviour.
Maybe you will neeed maybe to set async false in ajaxFileUpload.
Also please try first to make a working ASP NET MVC upload not using jqGrid form edit.
Best Regards
Tony
For professional UI suites for Java Script and PHP visit us at our commercial products site - guriddo.net - by the very same guys that created jqGrid.
19:36

21/06/2012

The problem was with the controller. jqGrid sends the files just fine, but it packs them into the request rather than passing back the file as an argument. The solution can be found by looking at the controller code here:
https://github.com/maxpavlov/jQuery-File-Upload.MVC3/blob/master/jQuery-File-Upload.MVC3/Controllers/HomeController.cs
Most Users Ever Online: 715
Currently Online:
42 Guest(s)
Top Posters:
OlegK: 1255
markw65: 179
kobruleht: 144
phicarre: 132
YamilBracho: 124
Renso: 118
Member Stats:
Guest Posters: 447
Members: 11373
Moderators: 2
Admins: 1
Forum Stats:
Groups: 1
Forums: 8
Topics: 10592
Posts: 31289
Newest Members:
, razia, Prankie, psky, praveen neelam, greg.valainis@pa-tech.comModerators: tony: 7721, Rumen[Trirand]: 81
Administrators: admin: 66