ユーザーが自分のプロフィール情報を編集できるビューがあります。ここには入力テキスト フィールドがあり、プロフィール画像を追加することもできます。これを行うには、次のようにかみそりで 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("");
}
これはうまくいきます。問題は、ユーザーが自分のプロフィールに画像を追加すると、
<input type="submit" class="bb-140" id="button7" value="Save"/>
ボタンは何もしません。ただし、ユーザーが画像を追加せずにプロファイルを編集すると、入力ボタンは本来の方法で送信されます。を使用して入力のクリック機能をバインドしてみました
$(document).on('click','#button7',function(e){
$("#SettingsForm").submit();
});
しかし、問題は解決しませんでした...この問題について何か助けていただければ幸いです。