Как показать загруженное изображение в виде изображения во время редактирования формы, а затем отправить это изображение в контроллер с помощью файла типа ввода.
Я пытаюсь, как показано ниже, но HttpPostedFileBase всегда возвращает нулевое значение.
Код моего просмотра
@using (Ajax.BeginForm("AddUpdateCategory", "Categories", FormMethod.Post, new AjaxOptions() { HttpMethod = "POST", OnComplete = "", OnSuccess = "", OnBegin = "", OnFailure = " }, new { @enctype = "multipart/form-data", id = "CategoryForm", novalidate = "novalidate" }))
{
<div class="file-upload ">
<button class="file-upload-btn" type="button" onclick="$('.file-upload-input').trigger( 'click' )">Add Image</button>
<div class="image-upload-wrap">
<input class="file-upload-input" type='file' name="file" onchange="readURL(this);" accept="image/*" />
<div class="drag-text">
<span> Drag and drop a file or select add Image</span>
</div>
</div>
<div class="file-upload-content">
<img class="file-upload-image" src="@Model.Base64String" alt="your image" />
@Html.HiddenFor(x=>x.FileName)
<div class="image-title-wrap">
<button type="button" onclick="removeUpload()" class="remove-image">Remove <span class="image-title">@Model.FileName</span></button>
</div>
</div>
</div>
}
Ниже приведен код моего контроллера.
public ActionResult AddUpdateCategory(Category objCategory, HttpPostedFileBase file, FormCollection fc)
{
using (HttpClient client = GetHttpClient())
{
try
{
if (file != null)
{
var filename = Path.GetFileName(file.FileName);
string extension = Path.GetExtension(file.FileName);
var fileid = Guid.NewGuid().ToString().Replace("-", "") + extension;
var path = Path.Combine(Server.MapPath("~/Uploads/Category/"), fileid);
file.SaveAs(path);
objCategory.ImageURL = fileid;
}
}
catch (Exception ex)
{
ModelState.AddModelError(string.Empty, "Error while uploading the files.");
}
}
return Json(new { ModelState }, JsonRequestBehavior.AllowGet);
}