2

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>
...
}

2 Answers 2

5

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).

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

5 Comments

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
do you know what caused that error? my DB attribute is varbinary(MAX) and Model is byte[]
@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?
I write exactly Like above, and when I save the file, the error occurs,
@WeakestInCoding See my edit above for the likeliest solution.
2

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");    
}

3 Comments

Then How do I store into the database next? like this? assignment.FileLocation = byteCount; db.Assignments.Add(assignment); db.SaveChanges();
@WeakestInCoding...above answer shown is basic concept of saving file .. for complete usage ...dotnet-tricks.com/Tutorial/mvc/…
@WeakestInCoding....just refer this post here....bluelemoncode.com/post/2011/11/16/…

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.