asp.net MVC upload file name is System.Web.HttpPostedFileWrapper

Viewed 54

AdminBlog is my controllers name. Blog is a table in my database. Foto is a column in the table.

when i saved the form, Foto column value become System.Web.HttpPostedFileWrapper. But others column's values are getting correct values. Also the image does not upload to BlogFoto file

Here is my model codes:

public partial class Blog
{
    public int BlogID { get; set; }
    public string BlogBaslik { get; set; }

    [UIHint("tinymce_full")][AllowHtml]
    public string BlogIcerik { get; set; }
    public string Foto { get; set; }
    public Nullable<int> BlogOkunmaSayisi { get; set; }
    public Nullable<int> BlogOkunmaSuresi { get; set; }
    public Nullable<System.DateTime> BlogTarih { get; set; }
}

and view codes:

@using (Html.BeginForm("Create", "AdminBlog",FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Yeni Blog Oluştur</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.BlogBaslik, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.BlogBaslik, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.BlogBaslik, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.BlogIcerik, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.BlogIcerik, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.BlogIcerik, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Foto, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <input type="file" name="Foto" class="form-control" />
            @Html.ValidationMessageFor(model => model.Foto, "", new { @class = "text-danger" })
        </div>
    </div>

 

    <div class="form-group">
        @Html.LabelFor(model => model.BlogOkunmaSuresi, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.BlogOkunmaSuresi, new { htmlAttributes = new { @class = "form-control", @placeholder = "DK cinsinden giriniz" } })
            @Html.ValidationMessageFor(model => model.BlogOkunmaSuresi, "", new { @class = "text-danger" })
        </div>
    </div>

and controllers:

 [HttpPost]
    public ActionResult Create(Blog blog, HttpPostedFile Foto)
    {
        try
        {
            
            if (Foto != null)
            {
                
                WebImage webImage = new WebImage(Foto.InputStream);
                string newfoto = Path.GetFileNameWithoutExtension(Foto.FileName) + "-" + Guid.NewGuid() + Path.GetExtension(Foto.FileName);
               
                var filePath = "/Uploads/BlogFoto" + newfoto;

                webImage.Save(Server.MapPath(filePath));
                blog.Foto = filePath;

            }
            
           
            blog.BlogOkunmaSayisi = 0;
            blog.BlogTarih = DateTime.Now;
            db.Blogs.Add(blog);
            db.SaveChanges();

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }
1 Answers

change Blog to this :

public partial class Blog
{
   //...............................
   //...............................
   public HttpPostedFileBase FotoFile { get; set; }
}

this code

<div class="form-group">
  @Html.LabelFor(model => model.Foto, htmlAttributes: new { @class = "control-label col-md-2" })
  <div class="col-md-10">
    <input type="file" name="Foto" class="form-control" /> 
    @Html.ValidationMessageFor(model => model.Foto, "", new { @class = "text-danger" })
  </div>
</div>

change to this :

<div class="form-group">
  @Html.LabelFor(model => model.FotoFile, htmlAttributes: new { @class = "control-label col-md-2" })
  <div class="col-md-10">
    @Html.TextBoxFor(model => model.FotoFile, new { type = "file", @class = "form-control" })
    @Html.ValidationMessageFor(model => model.FotoFile, null, new { @class = "text-danger" })
  </div>
</div>

and change controllert to this :

[HttpPost]
public ActionResult Create(Blog blog)
{
      try
      {
         if (blog.FotoFile != null)
         { 
            WebImage webImage = new WebImage(blog.FotoFile.InputStream);
            string newfoto = Path.GetFileNameWithoutExtension(blog.FotoFile.FileName) + "-" + Guid.NewGuid() + Path.GetExtension(blog.FotoFile.FileName);
               
            var filePath = "/Uploads/BlogFoto" + newfoto;
            webImage.Save(Server.MapPath(filePath));
            blog.Foto = filePath;
     }
     //..................................................................
     //..................................................................
     }
     catch
     {
         return View();
     }
 }
Related