MVC3 图片上传并显示,
有两个form表单,其中form1中有一“浏览”按钮,点击调用另一form(formimage)的<input file>控件,然后上传图片,再把图片显示在form1中的<img>中;用火狐可以实现,而用IE不能实现。
[code=C#][/code]
//后台代码
public void Upload()
{
if (Request.Files.Count > 0)
{
string nfname = Request.Files[0].FileName;
string path = AppDomain.CurrentDomain.BaseDirectory + "uploads\\";
string nsave = System.IO.Path.Combine(path, nfname);
Request.Files[0].SaveAs(nsave);
//返回文件路径
Response.Write("<script type='text/javascript'>parent.upcallback('../uploads/"+ Request.Files[0].FileName + "')</script>");
}
}
//前台js代码
[code=JScript][/code]
$(function () {
$("#upPhoto").bind({
click: function (event) {
$("#photo").click();
event.stopPropagation();
}
});
$("#photo").bind("change",function () {
$("#formimage").submit();
})
});
function upcallback(msg) {
$("#MphotoDis").attr("src",msg);
}
//前台formimage
[code=HTML][/code]
<form enctype="multipart/form-data" action="../MemberFiles/Upload" id="formimage" name="formimage" method="post" target="fileframe">
<input id="photo" name="photo" type="file" style="display:none" />
</form>
<iframe id="fileframe" name="fileframe" style=" display:none">
</iframe>
------解决方案--------------------Response.Write("<script type='text/javascript'>parent.upcallback('../uploads/"+ Request.Files[0].FileName + "')</script>");
改为
Response.Write("<script type='text/javascript'>window.onload = function() {parent.upcallback('../uploads/"+ Request.Files[0].FileName + "')};</script>");
------解决方案--------------------你传绝对路径啊 /upload/aa.jpg
------解决方案--------------------给你发个例子你看看,用的iframe
图片上传页面:
JScript code
function ImagePreview(f) {
if (f != "") {
document.getElementById("ImagePreview").src =<%=Url.Content("~/Upload/") %>+f;
document.getElementById("hd").value = f;
}
}
------解决方案--------------------
url.urlcontent()
看看这个。