Jquery 我无法在mvc中发布图像
我想创建一个表单,将图片上传到我的目录。使用ajax,我想在img标签的下方显示上传的图像,但我做不到。 我的代码: Ajax代码: 我的控制器:这是我将图像上传到文件夹的控制器。我将Json对象返回到我的ajaxJquery 我无法在mvc中发布图像,jquery,html,asp.net-mvc,Jquery,Html,Asp.net Mvc,我想创建一个表单,将图片上传到我的目录。使用ajax,我想在img标签的下方显示上传的图像,但我做不到。 我的代码: Ajax代码: 我的控制器:这是我将图像上传到文件夹的控制器。我将Json对象返回到我的ajax public ActionResult uploadpi(HttpPostedFileBase image) { JsonResult j = new JsonResult(); if (image != null &&
public ActionResult uploadpi(HttpPostedFileBase image)
{
JsonResult j = new JsonResult();
if (image != null && image.ContentLength > 0)
{
string fullpath = Path.Combine(Server.MapPath("~/Contents/"), Path.GetFileName(image.FileName));
if (System.IO.File.Exists(fullpath))
{
System.IO.File.Delete(fullpath);
}
image.SaveAs(fullpath);
}
j.Data = Path.GetFileName(image.FileName)
return (j);
}
我的表单:这是我上传图像的表单,它也有img标签来显示我上传的图像,但它不工作
<form method="post" enctype="multipart/form-data">
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>pic</legend>
<div class="editor-label">
@Html.LabelFor(model => model.picpath)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.picpath)
<input type="file" id="image" name="image" /><input type="button" value="upload" onclick="tempupl()" />
</div>
<img src="@Url.Content("a")" alt="" id="pic" name="pic"/>
我无法在ajax成功函数的@url.content中插入我的文件路径。我认为您不能仅使用ajax和通用HTML发布图像。 如果要在不刷新整个页面的情况下上载文件,有两种方法: [1] 将HTML5与ajax结合使用 [2] 使用iframe模拟ajax上传文件 我希望它能帮助你
<form method="post" enctype="multipart/form-data">
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>pic</legend>
<div class="editor-label">
@Html.LabelFor(model => model.picpath)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.picpath)
<input type="file" id="image" name="image" /><input type="button" value="upload" onclick="tempupl()" />
</div>
<img src="@Url.Content("a")" alt="" id="pic" name="pic"/>