Hey, I just solve my problems by using some different method
This is my model class named 'Picture'
public class Picture
{
public int PictureId { get; set; }
public IEnumerable<httppostedfile> Image { get; set; }
public string Name { get; set; }
public long Size { get; set; }
public string Path { get; set; }
}
</httppostedfile>
Then I create a controller named 'PictureController' and write 2 method inside there. I also create a folder named 'UplodedFiles' in project directory to save upload images in that folder.
[HttpPost]
public void Upload()
{
foreach (string file in Request.Files)
{
var postedFile = Request.Files[file];
postedFile.SaveAs(Server.MapPath("~/UploadedFiles/") + Path.GetFileName(postedFile.FileName));
}
}
public ActionResult List()
{
var uploadedFiles = new List<picture>();
var files = Directory.GetFiles(Server.MapPath("~/UploadedFiles"));
foreach(var file in files)
{
var fileInfo = new FileInfo(file);
var picture = new Picture() { Name = Path.GetFileName(file) };
picture.Size = fileInfo.Length;
picture.Path = ("~/UploadedFiles/") + Path.GetFileName(file);
uploadedFiles.Add(picture);
}
return View(uploadedFiles);
}
</picture>
Than create a 'Index' view to upload files
@model FileUploadAndRetrive.Models.Picture
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm("Upload", "Picture", FormMethod.Post, new { enctype="multipart/form-data" }))
{
<div>
Select a file: <input type="file" name="fileUpload" />
<input type="submit" value="Upload" />
</div>
}
Finally I create another view named 'List' for showing uploaded images
@model IEnumerable<fileuploadandretrive.models.picture>
@{
ViewBag.Title = "List";
}
<h2>List</h2>
<table>
<tr>
<td> Name </td>
<td> Size </td>
<td> Preview </td>
</tr>
@foreach (var file in Model)
{
<tr>
<td> @file.Name </td>
<td> @file.Size </td>
<td>
<img src="@Url.Content(file.Path)" />
</td>
</tr>
}
</table>
</fileuploadandretrive.models.picture>