Once you save the uploaded file on the server from your controller action you could pass back the url to this file to the view so that it can be displayed in an <img> tag:
public class FileUploadController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult FileUpload()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult FileUpload(HttpPostedFileBase uploadFile)
{
if (uploadFile.ContentLength > 0)
{
string relativePath = "~/img/" + Path.GetFileName(uploadFile.FileName);
string physicalPath = Server.MapPath(relativePath);
uploadFile.SaveAs(physicalPath);
return View((object)relativePath);
}
return View();
}
}
Then make your view strongly typed and add an <img> tag that will display the image if the model is not empty:
@model string
<h2>FileUpload</h2>
@using (Html.BeginForm("FileUpload", "FileUpload", FormMethod.Post, new { enctype = "multipart/form-data" })
{
<input name="uploadFile" type="file" />
<input type="submit" value="Upload File" />
}
@if (!string.IsNullOrEmpty(Model))
{
<img src="@Url.Content(Model)" alt="" />
}
No comments:
Post a Comment