109221793
109221793

Reputation: 16897

File Upload in MVC

Attempting to upload a file to a database and was wondering if someone could help me a bit as I've gotten a bit stuck, and there are no errors. What happens is, my view displays where I go to upload my file. As soon as I hit the upload button it brings me back to the CreateCover upload page of the application, however it fails to upload anything to the database.

If anyone can offer any help I'd be delighted :)

PS: http://www.mikesdotnetting.com/Article/125/ASP.NET-MVC-Uploading-and-Downloading-Files -->This is the tutorial I was originally following. I was trying to use this as a base for what I wanted to do.

Here are the snippets of my code:

CoverController.cs

//
        //GET: /File/CreateCover
        public ActionResult CreateCover()
        {
            Cover cover = new Cover();

            return View(cover);
        }

        //
        //POST: /File/CreateCover
        [HttpPost]
        public ActionResult CreateCover(FormCollection formvalues)
        {
            Cover cover = new Cover();

            cover.CoverMimeType = Request.Files["CoverUpload"].ContentType;
            Stream fileStream = Request.Files["CoverUpload"].InputStream;
            cover.CoverFileName = Path.GetFileName(Request.Files["CoverUpload"].FileName);
            int fileLength = Request.Files["CoverUpload"].ContentLength;
            cover.CoverFileContent = new byte[fileLength];
            fileStream.Read(cover.CoverFileContent, 0, fileLength);

            filerepository.Save();

            return View(cover);
        }

Cover.cs

 [MetadataType(typeof(Cover_Validation))]
    public partial class Cover
    {
        //
    }

    public class Cover_Validation
    {
        [Required(ErrorMessage = "Please enter a file")]
        [StringLength(50, ErrorMessage = "You have not selected a cover image to upload")]
        public byte[] CoverFileContent;

        [Required(ErrorMessage = "A MimeType is required")]
        [StringLength(13, ErrorMessage = "Your file must contain a MimeType")]
        public string CoverMimeType { get; set; }

        [Required(ErrorMessage = "A Filename is required")]
        [StringLength(13, ErrorMessage = "Your file must have a filename")]
        public string CoverFileName { get; set; }
    }

Snippet from FileRepository relating to Adding Cover

//Insert Cover Data
public void AddCoverData(Cover cover)
{
    entities.Covers.AddObject(cover);
}

And finally here is the CreateCover view

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SampleApp.Models.Cover>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    CreateCover
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>CreateCover</h2>

    <% using (Html.BeginForm("CreateCover", "Cover", FormMethod.Post, new { enctype = "multipart/form-data" }))
       { %>
    <asp:Label ID="Label2" runat="server" Text="Please Select your eBook Cover" /><br />
    <input type="file" name="CoverUpload" /><br />
    <input type="submit" name="submit" id="Submit" value="Upload" />

    <% } %>

    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>

</asp:Content>

Upvotes: 0

Views: 1573

Answers (1)

ten5peed
ten5peed

Reputation: 15900

You're being redirected to the home page because that's where you are posting the form to.

You need to post to the CreateCover action on your CoverController.

E.g.

<% using (Html.BeginForm("CreateCover",
                         "Cover",
                         FormMethod.Post,
                         new { enctype = "multipart/form-data" })) { %>

HTHs,
Charles

Upvotes: 4

Related Questions