imageuploader in MVC3http://forums.asp.net/t/1798586.aspx/1?imageuploader+in+MVC3Wed, 02 May 2012 03:24:49 -040017985864958712http://forums.asp.net/p/1798586/4958712.aspx/1?imageuploader+in+MVC3imageuploader in MVC3 <p>Hey i am new in MVC3.I &nbsp;have to develope an image uploader and jquery type thing in MVC3 as we can do inAsp.net..Can anybody help me??</p> 2012-04-30T17:53:38-04:004958935http://forums.asp.net/p/1798586/4958935.aspx/1?Re+imageuploader+in+MVC3Re: imageuploader in MVC3 <p>Hi,</p> <p>There are many available already.</p> <p>Try uploadify.</p> <p>Si</p> 2012-04-30T20:20:23-04:004959912http://forums.asp.net/p/1798586/4959912.aspx/1?Re+imageuploader+in+MVC3Re: imageuploader in MVC3 <p>i have done this code.it saves images in Images folder.but now the question is how to display that image i-e src of image=??....</p> <p><strong>Controller code</strong></p> <p>[HttpPost]<br> public ActionResult Index(HttpPostedFileBase file)<br> {<br> string path = System.IO.Path.Combine(Server.MapPath(&quot;~/Images&quot;), System.IO.Path.GetFileName(file.FileName));<br> file.SaveAs(path);<br> ViewBag.Message = &quot;File uploaded successfully&quot;;<br> return View();<br> }</p> <p><strong>Veiw code</strong></p> <p>@{<br> ViewBag.Title = &quot;Home Page&quot;;<br> }</p> <p>@using (Html.BeginForm(&quot;Index&quot;, &quot;Home&quot;, FormMethod.Post, new { enctype = &quot;multipart/form-data&quot; }))<br> { <br> &lt;label for=&quot;file&quot;&gt;Upload Image:&lt;/label&gt;<br> &lt;input type=&quot;file&quot; name=&quot;file&quot; id=&quot;file&quot;/&gt;<br> &lt;input type=&quot;submit&quot; value=&quot;Upload Image&quot; /&gt;<br> &lt;br /&gt;<br> <br> }<br> &lt;/p&gt;</p> <p><strong><br> </strong></p> 2012-05-01T13:05:58-04:004960284http://forums.asp.net/p/1798586/4960284.aspx/1?Re+imageuploader+in+MVC3Re: imageuploader in MVC3 <p></p> <blockquote><span class="icon-blockquote"></span> <h4>BushraWaris</h4> now the question is how to display that image</blockquote> <p></p> <p>&lt;img src='@Model.PathOfTheImage' /&gt;</p> 2012-05-01T16:39:32-04:004960545http://forums.asp.net/p/1798586/4960545.aspx/1?Re+imageuploader+in+MVC3Re: imageuploader in MVC3 <p>Thanks for replying.i tried this code but got an error i-e&nbsp;</p> <h2><i>Cannot perform runtime binding on a null reference</i></h2> <p><span></span><span face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif " style="font-family:Arial,Helvetica,Geneva,SunSans-Regular,sans-serif"><b>Description:&nbsp;</b>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.&nbsp;<br> <br> <b>Exception Details:&nbsp;</b>Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference</span></p> 2012-05-01T20:25:25-04:004960572http://forums.asp.net/p/1798586/4960572.aspx/1?Re+imageuploader+in+MVC3Re: imageuploader in MVC3 <p>It's null because you are not passing back a model with the path to your file, you are just rendering the View using View(). Ideally you want a strongly typed model with a property to the file.</p> <p>However you can also use the ViewBag to send data to the view with the filename. For example:</p> <p>In your controller</p> <pre class="prettyprint">[HttpPost] public ActionResult Index(HttpPostedFileBase file) { string path = System.IO.Path.Combine(Server.MapPath(&quot;~/Images&quot;), System.IO.Path.GetFileName(file.FileName)); file.SaveAs(path); ViewBag.Message = &quot;File uploaded successfully&quot;; ViewBag.FileName= file.FileName; return View(); }</pre> <p>In your view<br />&nbsp;</p> <pre class="prettyprint">&lt;img src='/images/@ViewBag.FileName' /&gt;</pre> <p></p> 2012-05-01T20:55:48-04:004960746http://forums.asp.net/p/1798586/4960746.aspx/1?Re+imageuploader+in+MVC3Re: imageuploader in MVC3 <p></p> <blockquote><span class="icon-blockquote"></span> <h4>BushraWaris</h4> i tried this code but got an error i-e Cannot perform runtime binding on a null reference</blockquote> <p></p> <p>Do you pass back a Model? A path to image into the Model?<br> Did you tried my code without making any changes to the action?!</p> <p></p> 2012-05-02T03:24:49-04:00