آپلود فایل توسط فرمهای پویای jqGrid
نویسنده: وحید نصیری
تاریخ: ۱۳۹۳/۰۴/۲۴ ۱۱:۳۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
<link href="~/Content/themes/base/jquery.ui.all.css" rel="stylesheet" />
<link href="~/Content/jquery.jqGrid/ui.jqgrid.css" rel="stylesheet" />
<link href="~/Content/Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div>
@RenderBody()
</div>
<script src="~/Scripts/jquery-1.7.2.min.js"></script>
<script src="~/Scripts/jquery-ui-1.8.11.min.js"></script>
<script src="~/Scripts/i18n/grid.locale-fa.js"></script>
<script src="~/Scripts/jquery.jqGrid.src.js"></script>
<script src="~/Scripts/ajaxfileupload.js"></script>
<script src="~/Scripts/jquery.blockUI.js"></script>
@RenderSection("Scripts", required: false)
</body>
</html> PM> Install-Package jQuery.BlockUI
<div dir="rtl" id="grid1" style="width:100%;" align="center">
<div id="rsperror"></div>
<table id="list" cellpadding="0" cellspacing="0"></table>
<div id="pager" style="text-align:center;"></div>
</div>
<script type="text/javascript">
$(document).ready(function () {
// Responsive jqGrid
$(window).bind('resize', function () {
var targetContainer = "#grid1";
var targetGrid = "#list";
$(targetGrid).setGridWidth($(targetContainer).width() - 2, true);
}).trigger('resize');
$('#list').jqGrid({
caption: "آزمایش هفتم",
/// .....
}).navGrid(
/// .....
).jqGrid('gridResize', { minWidth: 400, minHeight: 150 });
});
</script> colModel: [
{
name: '@(StronglyTyped.PropertyName<Product>(x=>x.ImageName))',
index: '@(StronglyTyped.PropertyName<Product>(x => x.ImageName))',
align: 'center', width: 220,
editable: true,
edittype: 'file',
formatter: function (cellvalue, options, rowObject) {
return "<img src='/images/" + cellvalue + "?rnd=" + new Date().getTime() + "' />";
},
unformat: function (cellvalue, options, cell) {
return $('img', cell).attr('src').replace('/images/', '');
}
}
], colModel: [
{
name: 'myac', width: 80, fixed: true, sortable: false,
resize: false, formatter: 'actions',
formatoptions: {
keys: true,
afterSave: function (rowid, response) {
doInlineUpload(response, rowid);
},
delbutton: true,
delOptions: {
url: "@Url.Action("DeleteProduct","Home")"
}
}
}
], $('#list').jqGrid({
caption: "آزمایش هفتم",
// ....
}).navGrid(
'#pager',
//enabling buttons
{ add: true, del: true, edit: true, search: false },
//edit option
{
width: 'auto',
reloadAfterSubmit: true, checkOnUpdate: true, checkOnSubmit: true,
beforeShowForm: function (form) {
centerDialog(form, $('#list'));
},
afterSubmit: doFormUpload,
closeAfterEdit: true
},
//add options
{
width: 'auto', url: '@Url.Action("AddProduct","Home")',
reloadAfterSubmit: true, checkOnUpdate: true, checkOnSubmit: true,
beforeShowForm: function (form) {
centerDialog(form, $('#list'));
},
afterSubmit: doFormUpload,
closeAfterAdd: true
},
//delete options
{
url: '@Url.Action("DeleteProduct","Home")',
reloadAfterSubmit: true
}).jqGrid('gridResize', { minWidth: 400, minHeight: 150 }); function doInlineUpload(response, rowId) {
return doUpload(response, null, rowId);
}
function doFormUpload(response, postdata) {
return doUpload(response, postdata, null);
}
function doUpload(response, postdata, rowId) {
// دریافت خروجی متد ثبت اطلاعات از سرور
// و استفاده از آی دی رکورد ثبت شده برای انتساب فایل آپلودی به آن رکورد
var result = $.parseJSON(response.responseText);
if (result.success === false)
return [false, "عملیات ثبت موفقیت آمیز نبود", result.id];
var fileElementId = '@(StronglyTyped.PropertyName<Product>(x=>x.ImageName))';
if (rowId) {
fileElementId = rowId + "_" + fileElementId;
}
var val = $("#" + fileElementId).val();
if (val == '' || val === undefined) {
// فایلی انتخاب نشده
return [false, "لطفا فایلی را انتخاب کنید", result.id];
}
$('#grid1').block({ message: '<h4>در حال ارسال فایل به سرور</h4>' });
$.ajaxFileUpload({
url: "@Url.Action("UploadFiles", "Home")", // مسیری که باید فایل به آن ارسال شود
secureuri: false,
fileElementId: fileElementId, // آی دی المان ورودی فایل
dataType: 'json',
data: { id: result.id }, // اطلاعات اضافی در صورت نیاز
complete: function () {
$('#grid1').unblock();
},
success: function (data, status) {
$("#list").trigger("reloadGrid");
},
error: function (data, status, e) {
alert(e);
}
});
return [true, "با تشکر!", result.id];
}
[HttpPost]
public ActionResult AddProduct(Product postData)
{
// ...
return Json(new { id = postData.Id, success = true }, JsonRequestBehavior.AllowGet);
}
[HttpPost]
public ActionResult EditProduct(Product postData)
{
// ...
return Json(new { id = postData.Id, success = true }, JsonRequestBehavior.AllowGet);
}
// todo: change `imageName` according to the form's file element name
[HttpPost]
public ActionResult UploadFiles(HttpPostedFileBase imageName, int id)
{
// ....
return Json(new { FileName = product.ImageName }, "text/html", JsonRequestBehavior.AllowGet);
} TypeError: jQuery.handleError is not a function jQuery.handleError(s, xml, status, e);
jQuery.extend({
handleError: function( s, xhr, status, e ) {
// If a local callback was specified, fire it
if ( s.error ) {
s.error.call( s.context || window, xhr, status, e );
}
// Fire the global callback
if ( s.global ) {
(s.context ? jQuery(s.context) : jQuery.event).trigger( "ajaxError", [xhr, s, e] );
}
}, SyntaxError: expected expression, got '<'
return Json(new { FileName = product.ImageName }, "text/html", JsonRequestBehavior.AllowGet);
if (imageName == null)
return Json(new { success = false }, JsonRequestBehavior.AllowGet); return Json(new { success = false }, "text/html", JsonRequestBehavior.AllowGet); // todo: change `imageName` according to the form's file element name
fileElementId: fileElementId, // آی دی المان ورودی فایل
function doUpload(response, postdata, rowId) {
var result = $.parseJSON(response.responseText);
if (result.id < 1)
return [false, "عملیات ثبت موفقیت آمیز نبود", result.id];
var fileElementId = 'xThumbnail';
if (rowId) {
fileElementId = rowId + "_" + fileElementId;
}
var val = $("#" + fileElementId).val();
if (val == '' || val === undefined) {
return [false, "لطفا فایلی را انتخاب کنید", result.id];
}
alert(fileElementId);
$('#grid1').block({ message: '<h4>در حال ارسال فایل به سرور</h4>' });
$.ajaxFileUpload({
url: "@Url.Action("UploadFile", "ProductTypes", new { area = "Admin" })",
secureuri: false,
fileElementId: fileElementId,
dataType: 'json',
data: { id: result.id },
complete: function () {
$('#grid1').unblock();
},
success: function (data, status) {
$("#grid").trigger("reloadGrid");
},
error: function (data, status, e) {
alert(e);
}
});
return [true, "با تشکر!", result.id];
} // todo: change `imageName` according to the form's file element name
[HttpPost]
public ActionResult UploadFiles(HttpPostedFileBase xThumbnail, int id)
{