我有一個視圖,用戶可以編輯他的個人資料信息。這裏是輸入文本字段,他還可以添加個人資料圖片。要做到這一點,我做了以下使用剃刀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