我試圖上傳圖像到我的文件夾,並在視圖中顯示。 我的控制器:顯示上傳的圖像asp.net mvc
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
if (file != null)
{
string pic = System.IO.Path.GetFileName(file.FileName);
string path = System.IO.Path.Combine(Server.MapPath("~/images"), pic);
file.SaveAs(path);
ViewBag.Path = path;
}
return View();
}
我的觀點:
@{
ViewBag.Title = "Upload";
}
@using (Html.BeginForm("Upload", "Home", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
<label for="file">Upload Image:</label>
<input type="file" name="file" id="file" style="width: 100%;" />
<input type="submit" value="Upload" class="submit" />
}
<h2>Upload</h2>
<img src="@ViewBag.path" alt="Image"/>
通過這樣做,圖像是不是在我看來,只有替代文字。生成的HTML源代碼:
<img src="C:\Users\D Stag\Documents\Visual Studio 2012\Projects\Data\Web\images\1232743916lituviai23afp.jpg"/>
圖像被正確保存,它存在於該文件夾中。
不能使用物理文件路徑SRC或HREF ......你需要使用絕對或相對路徑。 – MikeSmithDev