사용자가 자신의 프로필 정보를 수정할 수있는보기가 있습니다. 다음은 입력 텍스트 필드이며 프로필 이미지를 추가 할 수도 있습니다. 이렇게하려면 내가했던 다음과 같이 면도칼로 ASP .NET MVC3를 사용하여 다음보기에서 파일 업로드 후 양식이 제출되지 않습니다.
:<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>
으로 :
@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 부분보기에서이 이미지를 처리하기 위해 이러한 스크립트 :
<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("");
}
잘 작동합니다. 문제는 사용자가 프로필에 이미지를 추가하면
<input type="submit" class="bb-140" id="button7" value="Save"/>
버튼이 아무 것도하지 않는다는 것입니다. 그러나 사용자가 이미지를 추가하지 않고 자신의 프로파일을 편집하면 입력 버튼이 원하는 방식으로 제출됩니다. 나는
$(document).on('click','#button7',function(e){
$("#SettingsForm").submit();
});
를 사용하여 입력의 클릭 기능을 결합하려고하지만 문제는 내가이 문제에 어떤 도움을 부탁드립니다 ... 을 지속.
이미지를 이렇게 처리하는 이유는 사용자가 미리보기를 선택하면 페이지에서 동적으로 다시로드해야하기 때문입니다. 당신이 내가 이미지를 처리하려고했을 때 두 양식이 제출 될 것이라고 제안한 것을 시도했을 때. – Donnie
내가 제출할 사용자 데이터 양식의 이름 레이블 (예 :) 옆에있는 이미지를 미리 볼 수있는 간단한 예제를 보여 주면 크게 감사하겠습니다. 내가 너 너무 많은 시간을 가져 가면 미안해. – Donnie
좋습니다. 그것을 시도하고 알려 드리겠습니다. 다시 한 번 감사드립니다 – Donnie