MVC与DWZ整合中部分问题的解决

时间:2024-09-30 15:33:38

1、错误提示距离太远

2、正确与错误时返回JSON(即:如何不出现打开新页显示JSON字串)

3、打开新页后“数据加载中,请稍候”关不掉

4、如何正常的分页,同时如果有查询结果时分页也要有效

5、关闭弹出的对话框后出现半透明黑色框

6、DWZ使用<input type="file">上传文件返回JSON串IE无法解析问题

上传的页面的form中如果加上onsubmit = "return validateCallback(this, dialogAjaxDone)"是无法实现文件上传的,接收到的只能是null,所以需要换一种方式,即:

onsubmit="return iframeCallback(this,dialogAjaxDone);

[HttpPost]
[Description("接收商户标题图片")]
public string UploadTitle(HttpPostedFileBase upImg, string id)
{
int MaxSize = ; // 最大上传大小,默认是0.5M
string newFile = "";
string ext = "";
string fn = "";
string tmp = "";
try
{
//HttpFileCollectionBase fc = Request.Files;
//HttpPostedFileBase f = Request.Files[0];
if (upImg.ContentLength > MaxSize || upImg.ContentLength == )
{
tmp = "{ \"statusCode\" : \"200\", " +
"\"message\" : \"上传失败!文件大小不正确,必须是在0字节以上,0.5M字节以内\"," +
"\"navTabId\" : \"\"," +
"\"rel\" : \"\"," +
"\"callbackType\" : \"closeCurrent\"," +
"\"forwardUrl\": \"\"" +
"}";
return tmp;
}
if (!Directory.Exists(Server.MapPath("~/Upload")))
Directory.CreateDirectory(Server.MapPath("~/Upload"));
ext = upImg.FileName.Substring(upImg.FileName.LastIndexOf("."));
fn = DateTime.Now.ToString("yyyMMddHHmmssfff");
newFile = Server.MapPath("~/Upload") + "\\" + fn + ext;
upImg.SaveAs(newFile);
int dbid = Convert.ToInt32(id);
var data = db.Seller.Where(p => p.id == dbid).FirstOrDefault();
if (data != null)
{
data.TitleJPG = "/Upload/" + fn + ext;
db.SaveChanges();
}
//AJAX不支持上传文件,所以用了隐藏iframe方式,但返回就不能是JSON格式,只能返回字符串格式
tmp = "{ \"statusCode\" : \"200\", " +
"\"message\" : \"修改成功\"," +
"\"navTabId\" : \"dlg_modiSeller\"," +
"\"rel\" : \"\"," +
"\"callbackType\" : \"closeCurrent\"," +
"\"forwardUrl\": \"\""+
"}";
return tmp; }
catch (Exception ex)
{
tmp = "{ \"statusCode\" : \"200\", " +
"\"message\" : \"上传图片失败\"," +
"\"navTabId\" : \"\"," +
"\"rel\" : \"\"," +
"\"callbackType\" : \"closeCurrent\"," +
"\"forwardUrl\": \"\"" +
"}";
return tmp;
} }

后台接收文件并按ID修改数据库

<form action="@Url.Action("UploadTitle", "Seller", new { id = ViewData["id"].ToString() })" method="post" enctype="multipart/form-data" class="pageForm required-validate" onsubmit="return iframeCallback(this,dialogAjaxDone);" >
<input type="file" name="upImg" id="upImg" style="width: 180px" />
<button type="submit">文件上传</button>
</form>

前台上传代码

注意点:前台的input控件名与后台接收的一致,即(HttpPostedFileBase upImg, string id)中的upImg必须与前台的一致,否则接收不到文件

相关文章