i'm new mvc, , attempting build page upload multiple images site. however, request.files collection contains strings, containing "fileupload". have read many of multiple file uploads site, none appear work. i'm not i'm going wrong , appreciate experienced eyes assist. once can proper file info, can upload site.
thank assistance, allan
source files included below.
imagescontroller.cs
using system; using system.collections.generic; using system.linq; using system.web; using system.web.mvc; namespace myapp.controllers { public class imagescontroller : controller { // get: images/upload [httpget] public actionresult upload() { return view(); } // post: images/upload [httppost] public actionresult upload(formcollection formcollection) { try { httpfilecollectionbase files = request.files; foreach(httppostedfilebase file in files) { int length = file.contentlength; string type = file.contenttype; string filename = file.filename; } return redirecttoaction("upload"); } catch { return view(); } } } }
upload.cshtml
@{layout = null;} <!doctype html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>upload</title> </head> <body> <form method="post" action="~/images/upload" id="form1" enctype="multipart/form-data"> <table style="margin:1px; background-color:#ccc;"> <tr style="background-color:#fff;"> <td><h1>file upload form</h1></td> <td rowspan="3"> @if (viewdata.model != null) { foreach (var item in viewdata.model) { <img src="/images/@item["path"]" alt="fileupload image" /> } } </td> </tr> <tr style="background-color:#fff;"> <td> <input type="file" name="fileupload" id="fileupload" multiple="multiple" /> </td> </tr> <tr style="background-color:#fff;"> <td> <input type="submit" name="btnsubmit" value="upload" id="btnsubmit" /> </td> </tr> </table> </form> </body> </html>
the answer has been found, had 2 major issues causing code not operate expected. method declaration flawed, should read as:
public actionresult upload(ienumerable<httppostedfilebase> uploadfiles)
secondly, input name in view , parameter in declaration must match. changing view use:
<input type="file" name="uploadfiles" id="fileupload" multiple="multiple" />
making these changes, allowed controller cycle through images.
thank you, allan
Comments
Post a Comment