Html.BeginForm("AddImg","admin",FormMethod.Post,new { @enctype="multipart/form-data"}用以上MVC非AJAX能获取其中的<input type="file" id="f1" name="f1" />
换成
Ajax.BeginForm("AddImg","", new AjaxOptions { UpdateTargetId = "myimg" }, new { @enctype = "multipart/form-data" })之前就没办法获取<input type="file" id="f1" name="f1" />
了,不知道是不是哪里写错了,请高手指点!
换成
Ajax.BeginForm("AddImg","", new AjaxOptions { UpdateTargetId = "myimg" }, new { @enctype = "multipart/form-data" })之前就没办法获取<input type="file" id="f1" name="f1" />
了,不知道是不是哪里写错了,请高手指点!
推测
HttpMethod,需设置下
dataType: 'json',
beforeSubmit: _this.showRequest, //提交前处理
error: _this.showError, //错误处理
success: _this.showResponse //提交成功的处理
};
$("#myForm").ajaxForm(options);cs代码处理public ContentResult DoSaveImg()
{
....Request.Files //保存处理
return Content("成功", "text/html");
}
dataType: 'json',
beforeSubmit: _this.showRequest, //提交前的处理
error: _this.showError, //出错的处理
success: _this.showResponse //提交成功后的处理
};
$("#myForm").ajaxForm(options);cs代码处理,public ContentResult DoSaveHotelCartea()
{
.....Request.Files //保存图片处理
return Content("成功", "text/html");
}。
纯ajax没有办法执行文件上传你要上传文件必须有一个隐藏的frame 通过这个子frame上传 然后向子写入js代码来通知页面前台的代码:<div id="UpLoad_Show"></div><script type="text/javascript">
function UpLoadPictureShow(id, ifVertically) {
var value = $("#pictures").val();
var img;
if (ifVertically == "Vertically") {
img = "<img alt='' src='/File/DownLoad/" + id + "' width='90px' height='120px' /> ";
}
else {
img = "<img alt='' src='/File/DownLoad/" + id + "' width='120px' height='90px' /> ";
}
$("#UpLoad_Show").append(img);
if (value == 0) {
value = id;
}
else {
value += "," + id;
}
$("#pictures").val(value);
}
</script><div>
<form action="/File/UpLoadPicture" method="post" enctype="multipart/form-data" target="UpLoad_Do">
<input name="File1" type="file" />
<input type="submit" value="上传" />
</form>
<iframe id="UpLoad_Do" src="../Child/UpLoad.htm" style="display:none"></iframe>
</div>
后台的代码:public void UpLoadPicture()
{
HttpPostedFileBase i = Request.Files[0]; //限定文件大小
if (i.ContentLength > 0 && i.ContentLength <= WebSetting.fileSize)
{
Regex reg = new Regex(@".\w+$");
string fileName = reg.Replace(i.FileName, ""); FileForSet t = new FileForSet();
t.fileName = fileName;
t.mime = i.ContentType;
t.useFor = "picture"; int fileId = FileManager.Create(t, (Author)Session["userInfo"]); //把上传的图片转为符合要求的缩略图
System.Drawing.Bitmap img = new System.Drawing.Bitmap(i.InputStream); #region 等比缩放 int theHeight = WebSetting.imgHeight;
int theWight = WebSetting.imgWight;
int newHeight;
int newWight; bool Vertically = (img.Height >= img.Width) ? true : false; if (Vertically)
{
theHeight = WebSetting.imgWight;
theWight = WebSetting.imgHeight;
} if (img.Height > img.Width * theHeight / theWight)
{
newHeight = (img.Height >= theHeight) ? theHeight : img.Height;
newWight = img.Width * newHeight / img.Height;
}
else
{
newWight = (img.Width >= theWight) ? theWight : img.Width;
newHeight = img.Height * newWight / img.Width;
} #endregion System.Drawing.Image temp = img.GetThumbnailImage(newWight, newHeight, null, new IntPtr());
string path = Server.MapPath("~/UpLoadFiles/File_" + fileId);
FileManager.SavePath(fileId, path, (Author)Session["userInfo"]);
temp.Save(path); //通告页面上传已经完成
if (Vertically)
{
Response.Write("<script type='text/javascript'>window.parent.UpLoadPictureShow('" + fileId + "','Vertically');</script>");
}
else
{
Response.Write("<script type='text/javascript'>window.parent.UpLoadPictureShow('" + fileId + "','NoVertically');</script>");
}
}
}