我有一个视图,用户可以编辑他的个人资料信息。这里是输入文本字段,他还可以添加个人资料图片。要做到这一点,我做了以下使用剃刀ASP .NET MVC3如下:表单不会在文件上传后提交
在查看:
@using (Html.BeginForm("SettingsWizard", "Business", FormMethod.Post, new { enctype = "multipart/form-data", id="SettingsForm" }))
{
@Html.Partial("_UsrConfiguration", Model)
@Html.Partial("_OtherPartialView")
@Html.Partial("_OtherPartialViewTwo")
<p class="textAlignRight">
<input type="submit" class="bb-140" id="button7" value="Save"/>
</p>
<div class="clear"></div>
}
在_UsrConfiguration局部视图来处理图像:
<div class="floatRight" >
<a onclick="javascript:opendialogbox('imageLoad2');" style="cursor: pointer">Foto</a>
<img src="../../Content/themes/base/images/icons/zoom.png" width="16" height="16" id="imgThumbnail2" alt="foto" />
<input type="file" name="imageLoad2" accept="image/*" id="imageLoad2" onchange="ChangeProfileImage()" hidden="hidden" />
</div>
随着这些脚本:
<script type="text/javascript">
function ChangeProfileImage() {
var ext = document.getElementById('imageLoad2').value.match(/\.(.+)$/)[1];
switch (ext.toLowerCase()) {
case 'jpg':
case 'bmp':
case 'png':
case 'gif':
case 'jpeg':
{
var myform = document.createElement("form");
myform.style.display = "none";
myform.action = "/ImagePreview/ProfileImageSubmit";
myform.enctype = "multipart/form-data";
myform.method = "post";
var imageLoad;
var imageLoadParent;
var is_chrome = /chrome/.test(navigator.userAgent.toLowerCase());
if (is_chrome && document.getElementById('imageLoad2').value == '')
return; //Chrome bug onchange cancel
if (document.all || is_chrome) {//IE
imageLoad = document.getElementById('imageLoad2');
imageLoadParent = document.getElementById('imageLoad2').parentNode;
myform.appendChild(imageLoad);
document.body.appendChild(myform);
}
else {//FF
imageLoad = document.getElementById('imageLoad2').cloneNode(true);
myform.appendChild(imageLoad);
document.body.appendChild(myform);
}
$(myform).ajaxSubmit({ success:
function (responseText) {
var d = new Date();
$("#imgThumbnail2")[0].src = "/ImagePreview/ProfileImageLoad?a=" + d.getMilliseconds();
if (document.all || is_chrome)//IE
imageLoadParent.appendChild(myform.firstChild);
else//FF
document.body.removeChild(myform);
}
});
}
break;
default:
alert('File type error…');
}
}
</script>
在控制器端处理图像:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ProfileImageSubmit(int? id)
{
Session["ContentLength"] = Request.Files[0].ContentLength;
Session["ContentType"] = Request.Files[0].ContentType;
byte[] b = new byte[Request.Files[0].ContentLength];
Request.Files[0].InputStream.Read(b, 0, Request.Files[0].ContentLength);
//DB saving logic and persist data with…
repo.Save();
return Content(Request.Files[0].ContentType + ";" + Request.Files[0].ContentLength);
}
public ActionResult ProfileImageLoad(int? id)
{
TCustomer usr = new TCustomer();
usr = repo.load(User.Identity.Name);
byte[] b = usr.ContactPhoto;
string type = (string)Session["ContentType"];
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = type;
Response.BinaryWrite(b);
Response.Flush();
Response.End();
Session["ContentLength"] = null;
Session["ContentType"] = null;
return Content("");
}
这工作正常。问题是,如果用户将图像添加到他的个人资料中,那么按钮不会执行任何操作。但是,如果用户在不添加图像的情况下编辑他的个人资料,输入按钮就会按照它的方式提交。我试着用结合
$(document).on('click','#button7',function(e){
$("#SettingsForm").submit();
});
输入的点击功能,但问题依然存在...... 我很感激这个问题的任何帮助。
这样处理图像的原因是因为我需要在用户选择它(预览)后在页面上动态地重新加载。当我尝试做你所建议的两种形式时,我会尝试处理图像。 – Donnie
如果你能告诉我一个简单的例子,我可以在要提交的用户数据表单的名称标签(例如)旁边预览图像,我将不胜感激。对不起,如果我占用你太多的时间。 – Donnie
好吧,这是有道理的。让我试试看,并让你知道。再次感谢 – Donnie