3

Basically what I have at the moment is a ASP.NET MVC project made database first the tables take alpha numeric input but I think it would be pretty nifty to have a file upload so I can insert a picture into the database so I created a table in SQL server with an ID field and an image field with the data type image.

When i add this model and create my controller and views based on that model it doesn't create the label or editor for this field, I used DataAnnotations to give it the DataType upload but I can't find a way of actually uploading the file I tried this which works for normal input but nothing shows up is there a way of having a @HTML.FileUploadFor(model => model.Vehicle) or something along those lines.

This is what I've tried

<div class="editor-label">
    @Html.LabelFor(model => model.Vehicle)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Vehicle)
    @Html.ValidationMessageFor(model => model.Vehicle)
</div>

This is my model

public partial class VehicleImage
{
    public int Vehicle_ID { get; set; }
    [DataType(DataType.Upload)]
    public byte[] Vehicle { get; set; }
}

3 Answers 3

3

There are many approaches to this.

I usually use a jquery plugin called Uploadify.

Have a look at this post - https://stackoverflow.com/a/17520062/1581026

Another way is just by adding the following in your view inside your form:

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

Your form element needs to look like the following:

 @using ( Html.BeginForm( "ACTION", "CONTROLLER", FormMethod.Post, new { enctype = "multipart/form-data" }))

And then on your model that is getting passed to your POST Action you can add a attribute:

public HttpPostedFileBase Picture {get; set; }

Or you can also just add it as a parameter in your POST Action:

[HttpPost]
Public ActionResult SomeAction(HttpPostedFileBase Picture, .....)
Sign up to request clarification or add additional context in comments.

5 Comments

Cheers, Picture in this instance being the name of the database field I'm uploading it into?
no, "Picture" will be an attribute in your model. You can't directly save it into your database. You will first have to change it into a byte array.
Sorry I forgot you also need to add encype = multipart/form-data as in Ajay Punekar's post - stackoverflow.com/a/20996333/1581026. I Also added it in my post.
I keep getting this error 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.
when I try to save it to the database
3

There isn't a standard control or helper for files as far as I know. However here is a nice tutorial to achieve what you want.

http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx/

Furthermore, if you really want this functionality, you can write an html helper yourself.

  1. by writing an extensionmethod to the HtmlHelper object
  2. By declaring a code block in your view eg:

@helper MyHelper(string id){ <input type="file" id="@id" /> }

1 Comment

Cheers I'll be looking into this
0

try something like this:

Create this type of view

    using (Html.BeginForm("FileUpload", "FileUpload", 
                FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
      <input name="uploadFile" type="file" />
      <input type="submit" value="Upload File" />
    }

Here is controller method

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult FileUpload(HttpPostedFileBase uploadFile)
{
}

Please refer this link : http://www.codeproject.com/Articles/38970/Implementing-HTTP-File-Upload-with-ASP-NET-MVC

Comments

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.