c# - How to upload "one" image to a database with Enitity Framework and MVC 4 -


i trying write image upload service postcontroller in create action, getting compile error saying:

'system.array' not contain definition 'inputstream'/contentlength , no extension method 'inputstream'/contentlength accepting first argument of type 'system.array'

on these lines:

    using (var binaryreader = new binaryreader(uploadimage.inputstream))     {         imagedata = binaryreader.readbytes(uploadimage.contentlength);     } 

i want upload 1 file , no more. unsure if correct: singlepart/form-data in html.begin form.

i did try httppostedfilebase uploadimage instead of httppostedfilebase[] uploadimage got error of instance not set object on line:

using (var binaryreader = new binaryreader(uploadimage.inputstream)) 

it should noted post.picture (varbinary(max)) in post table.

view:

@using (html.beginform("create", "post", formmethod.post, new { enctype = "singlepart/form-data" })) {     @html.antiforgerytoken()     @html.validationsummary(true)      <fieldset>              <div class="col-xs-6 col-md-4">                 @html.labelfor(model => model.picture)              <div class="editor-field">                 <input type="file" name="uploadimages" class="input-files" />                 @html.validationmessagefor(model => model.picture)             </div>         </div> 

controller:

    [authorize]     public actionresult create()     {         return view();     }     [httppost]     [validateantiforgerytoken]     [authorize]     public actionresult create([bind(include = "bloguseremail,categoryid,title,shortdescription,description")]post post, httppostedfilebase[] uploadimage, string selectedtags)     {         if (uploadimage != null)         {             return redirecttoaction("create");         }          byte[] imagedata = null;          using (var binaryreader = new binaryreader(uploadimage.inputstream)) // not set instance of object         {             imagedata = binaryreader.readbytes(uploadimage.contentlength);         }         var image = new post         {              picture = imagedata         };          post.picture = image.picture; 

you error because have array of objects , try use contentlength/inputstream property of httppostedfilebase

instead of uploadimage.inputstream can take first element if try tu upload 1 image uploadimage[0].inputstream or uploadimage.first().inputstream. or instead can remove [].

also have change enctype "singlepart/form-data" "multipart/form-data"

the input have name uploadimages , parameter of action uploadimage, change input according

<input type="file" name="uploadimage" class="input-files" /> 

i recommend use model encapsulate form elements.

public class postcreatemodel : post {     public httppostedfilebase uploadimage { get; set; }     public string selecttags { get; set; } } 

and action this. don't forget change @model in view.

public actionresult create(postcreatemodel post) {     if (post.uploadimage != null)     {         return redirecttoaction("create");     }      byte[] imagedata = null;      using (var binaryreader = new binaryreader(post.uploadimage.inputstream)) // not set instance of object     {         imagedata = binaryreader.readbytes(post.uploadimage.contentlength);     }     var image = new post     {          picture = imagedata     };      post.picture = image.picture; 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -