1

I'm trying to upload files in my application. I've used HttpPostedFileBase in my model and a byte[] array but don't know why this error is showing when i'm running my application. Below I've also uploaded the image of the error that is showing when running the app.

The error that is showing is:

One or more validation errors were detected during model generation:

AdSite.Models.HttpPostedFileBase: : EntityType 'HttpPostedFileBase' has no key defined. Define the key for this EntityType. HttpPostedFileBases: EntityType: EntitySet 'HttpPostedFileBases' is based on type 'HttpPostedFileBase' that has no keys defined.`

Error screenshot

My Model:

public class Album
{
    [Key]
    public int Id { get; set; }
    public string ProductTitle { get; set; }
    public string Description { get; set; }
    public string ImageFileName { get; set; }
    public int ImageSize { get; set; }
    public byte[] ImageData { get; set; }
    [Required(ErrorMessage="Please select image file.")]
    public HttpPostedFileBase File { get; set; }        
}

My controller code:

public ActionResult Upload([Bind(Include = "Id,ProductTitle,Description,ImageFileName,ImageData,File,ImageSize")]Album album)
{
    if (ModelState.IsValid)
    {
        //  album.ImageFileName = album.File.FileName;
        // album.ImageSize = album.File.ContentLength;

        byte[] data = new byte[album.File.InputStream.Length];
        album.File.InputStream.Read(data, 0, data.Length);
        album.ImageData = data;

        var db = new AlbumContext();
        db.Albums.Add(album);
        db.SaveChanges();

        return RedirectToAction("Index");
    }
    return View(album);
}

My View code:

<div class="form-group">
    <label class="control-label col-md-2">Select Image:</label> 
    <div class="col-md-10">
        @Html.TextBoxFor(model=>model.File, new { type="file"})
        @Html.ValidationMessage("CustomError")
    </div>
</div>
Community
  • 1
  • 1
smk pobon
  • 83
  • 1
  • 10
  • In the 'Bind' attribute, you define properties for 'album'. Where is the 'File' property coming from? – SRQ Coder Dec 08 '16 at 12:48
  • Hello @SRQCoder , the `File` property is defined in the `Model class`. it is of type `HttpPostedFileBase File`. You can see my model class here. – smk pobon Dec 08 '16 at 12:50
  • You need to remove `public HttpPostedFileBase File { get; set; }` from your data model (its a complex object and cannot be stored in a database column). You editing data so use a view model and the view model will contain that property (and not the `public byte[] ImageData { get; set; }` property - [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Dec 09 '16 at 00:42

1 Answers1

0

This is why you want to use Models to retrieve the data and then convert it to your DTO objects.

The error is because you're trying to store the System.Web.HttpPostedFileBase class into your database. That's not your class to control, and it wasn't created to be stored directly in a database. In this case, HttpPostedFileBase is your "model".

Create another object and tie it into your DbContext to store what you need to about the file in your database. Don't just toss the object in there.

krillgar
  • 12,596
  • 6
  • 50
  • 86
  • thanks. but can you please show me an example code of this here? – smk pobon Dec 08 '16 at 13:16
  • do you mean to create a new "model class" for other info about the file that need to upload? that is do i need two class, one for HttpPostedFileBase property and one for the other property?? – smk pobon Dec 08 '16 at 13:20
  • I meant create a DTO class for whatever info you need from `HttpPostedFileBase`. – krillgar Dec 08 '16 at 13:23
  • I've provided a link to the `HttpPostedFileBase` class. You already have your `Album` class, just do a similar process for your File. – krillgar Dec 08 '16 at 13:26
  • @krillgar Could you please help to resolve this [question](https://stackoverflow.com/questions/63602008/how-to-upload-folder-and-sub-folders-and-files-inside-the-sub-folders-in-asp-net) – Tech vidhyarthi Aug 27 '20 at 04:28
  • @SafeerMP That question is far too broad. It also may not be possible to upload "everything" do to different levels of Operating System and Browser security. – krillgar Aug 27 '20 at 14:21