Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Would someone mind to guide me, how to save file into my database, and also retrieve it if possible, I still new to this C# and MVC 4. my databse Assignment contain a attribute call FileLocation which is varBinary ( MAX) .

Model

 public partial class Assignment
{
    public Assignment()
    {
        this.CourseAvailables = new HashSet<CourseAvailable>();
    }



    public string AssignmentID { get; set; }
    public Nullable<System.DateTime> SubmissionDate { get; set; }
    public string Status { get; set; }
    [Range(0,100, ErrorMessage="Only Value between 0-100 is accepted.")]
    public Nullable<decimal> Mark { get; set; }
    public string Comments { get; set; }
    public byte[] FileLocation { get; set; }

    public virtual ICollection<CourseAvailable> CourseAvailables { get; set; }
}
}

Control

[HttpPost]
    public ActionResult Create(Assignment assignment)
    {

        if (ModelState.IsValid)
        {
            db.Assignments.Add(assignment);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(assignment);
    }

View

@using(Html.BeginForm("Create","Assignment",FormMethod.Post,new {enctype="multipart/form-data"}))
{
...
<div class="editor-field">
    <%: Html.TextBoxFor(model => model.FileLocation, new { type="file"})%>
    <%: Html.ValidationMessageFor(model => model.FileLocation) %>
</div>
...
}
share|improve this question
up vote 4 down vote accepted

You need a bit more processing here. Uploaded files come in as a HttpPostedFileBase, not a byte[] so you need to get the byte[] from the HttpPostedFileBase's InputStream, like so:

[HttpPost]
public ActionResult Create(Assignment assignment)
{
    if(Request.Files != null && Request.Files.Count == 1)
    {
        var file = Request.Files[0];
        if (file != null && file.ContentLength > 0)
        {
            var content = new byte[file.ContentLength];
            file.InputStream.Read(content, 0, file.ContentLength);
            assignment.FileLocation = content;

            // the rest of your db code here
        }
    }
    return RedirectToAction("Create");    
}

P.S. That model looks suspiciously like an Entity object. It's a tremendously bad idea to use Entity objects as your models. Try making an intermediary model and using that to render your data instead.

Edit

In your view, change this:

<%: Html.TextBoxFor(model => model.FileLocation, new { type="file"})%>

to this:

<input type="file" id="file" name="file" />

The error you're getting is because the model binder is mistakenly trying to bind your FileLocation field on the page (HttpPostedFileBase type) to the FileLocation field in your model (byte[] type).

share|improve this answer
    
The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. this error occurs when I try this code – WeakTaenie Aug 6 '14 at 14:08
    
do you know what caused that error? my DB attribute is varbinary(MAX) and Model is byte[] – WeakTaenie Aug 6 '14 at 15:20
    
@WeakestInCoding Do you know on what line of the code the error occurs? And can you show me your whole method as it's written now? – Radu Porumb Aug 6 '14 at 15:29
    
I write exactly Like above, and when I save the file, the error occurs, – WeakTaenie Aug 6 '14 at 15:33
    
@WeakestInCoding See my edit above for the likeliest solution. – Radu Porumb Aug 6 '14 at 15:50

In Asp.Net MVC we have to use HttpPostedFileBase for Uploaded files as shown below :-

[HttpPost]
public ActionResult Create(Assignment assignment, HttpPostedFileBase file)
{
  if (file != null)
        {
            int byteCount = file.ContentLength;   <---Your file Size or Length
            .............
            .............//You can store file in database//
        }
 return RedirectToAction("Create");    
}
share|improve this answer
    
Then How do I store into the database next? like this? assignment.FileLocation = byteCount; db.Assignments.Add(assignment); db.SaveChanges(); – WeakTaenie Aug 6 '14 at 13:05
    
@WeakestInCoding...above answer shown is basic concept of saving file .. for complete usage ...dotnet-tricks.com/Tutorial/mvc/… – Kartikeya Khosla Aug 6 '14 at 13:07
    
@WeakestInCoding....just refer this post here....bluelemoncode.com/post/2011/11/16/… – Kartikeya Khosla Aug 6 '14 at 15:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.