Forum

November 2nd, 2014
A A A
Avatar

Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

The forums are currently locked and only available for read only access
sp_Feed Topic RSS sp_Related Related Topics sp_TopicIcon
AjaxFileUpload failing for MVC3 jqGrid add method
17/08/2012
22:33
Avatar
rre9518
Member
Members
Forum Posts: 3
Member Since:
21/06/2012
sp_UserOfflineSmall Offline

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:

if( ret[0] && $.isFunction(rp_ge[$t.p.id].afterSubmit) ) {
ret = rp_ge[$t.p.id].afterSubmit.call($t, data,postdata);
}
ret in undefined?
 

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);

}

19/08/2012
15:29
Avatar
tony
Sofia, Bulgaria
Moderator
Members

Moderators
Forum Posts: 7721
Member Since:
30/10/2007
sp_UserOfflineSmall Offline

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/08/2012
19:36
Avatar
rre9518
Member
Members
Forum Posts: 3
Member Since:
21/06/2012
sp_UserOfflineSmall Offline

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

Forum Timezone: Europe/Sofia

Most Users Ever Online: 715

Currently Online:
60 Guest(s)

Currently Browsing this Page:
1 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.com

Moderators: tony: 7721, Rumen[Trirand]: 81

Administrators: admin: 66

Comments are closed.
Privacy Policy   Terms and Conditions   Contact Information