0

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>

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Charles, that looks about right. That'll teach me for copying and pasting! I'll give that a try and let you know how it goes.
Hi Charlino, I've changed this error, and spotted another error myself. It's amazing what a break away can do. My only problem is now, that when I click upload, it brings me back to the file upload page, which is the desired result, however it fails to upload anything to the database. Any ideas?
Just reading your action code, you call filerepository.Save(); but at no point are you attaching the new Cover cover model to your repository... do you need to call something like filerepository.Add(cover); before calling .Save();?
Hi Charlino, sorry about the delay, just discovered that last night before I went to bed. Amended it and that particular problem seems to be solved. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.