1

I have problem with uploading image to SQL database. i have Methods Upload in controller Upload

dbData userDb = new dbData();

public ActionResult Upload()
{
    return View();
}

[HttpPost]
public ActionResult Upload(HttpPostedFileWrapper file)
{
    if (file.ContentLength > 0)
    {
        Stream fileStream = file.InputStream;
        string fileName = Path.GetFileName(file.FileName);
        int fileLength = file.ContentLength;
        byte[] fileData = new byte[fileLength];
        fileStream.Read(fileData, 0, fileLength);

        var image = new ImageTable();
        image.Image = fileData;
        image.Description = "Default profile picture";

        try
        {
            userDb.ImageTables.InsertOnSubmit(image);
            userDb.SubmitChanges();
            return RedirectToAction("Success");
        }
        catch (Exception ex)
        {
            throw;
        }

    }
    return View();
}

If i use this view page

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Upload</title>
</head>
<body>
    <div>

     <% using (Html.BeginForm("Upload", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {%>
        <input name="file" type="file" runat="server" id="file"/><br />
        <input type="submit" value="Upload File" />
<%} %>
    </div>
</body>
</html>

everithing works great but if i want to use view runned on masterpage i wil get this error after i click upload submit button:

No parameterless constructor defined for this object.

Know someone where is problem and how can i fix it ? Thanks

1 Answer 1

1

This error occurs because default model binder can't create instances of classes that don't contain parameterless constructor (such as HttpPostedFileWrapper).

Simplest way around this is to just pull files from Request.Files (e.g. Request.Files["file"]).

Alternatively, you could create custom model binder for that.

UPDATE:

This is action method I used:

[HttpPost]
public ActionResult Index(FormCollection form)
{
    var file = Request.Files["file"];
    if(file != null && file.ContentLength > 0)
    {
        // ...
    }

    return View();
}
Sign up to request clarification or add additional context in comments.

3 Comments

if i use Request.Files like..> public ActionResult Uploads(FormCollection { if (Request.Files["file"].ContentLength > 0)...bla bla bla i get error on "IF" line SystemNullReference exception >Object reference not set to an instance of an object.
It should work, you must be doing something else wrong. I pasted the code in your question into a view, setup a post action, selected a file, submitted, and it worked.
can you wrote me post action script u used ? I added this code: foreach (string postedFile in Request.Files){ HttpPostedFileBase file = Request.Files[postedFile]; if (file.ContentLength > 0) and it works but when i am using code witout foreach cycle it isn't working

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.