Click here to Skip to main content
15,885,874 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want to upload a file in my project, the file path and name should be saved in database, but I cant get the right solution.

Here is my views code -
HTML
<table>
       <tr>
       <td class="EmptyClass"></td>
       <td class="LabelCalss"><label>Attachments</label></td>

       <td class="TextBoxClass">

     <input type="file" id="file"/>

       <input type="button" id="btnup" value="Upload" />  </td>     </tr>
       <tr><td class="EmptyClass"></td><td class="LabelCalss"></td>



       <td class="text-box">
       @Html.ListBox("UploadFiles", Enumerable.Empty<SelectListItem>())
       </td>
       </tr></table>

When I click on upload button it will save in DB as I need.
Posted
Updated 14-Feb-12 1:16am
v2

Make you form like this
C#
@using (Html.BeginForm("ActionResult", "Controller", FormMethod.Post, new { @enctype = "multipart/form-data" }))


and in you Actionresult pass a parameter

C#
Actionresult(HttpPostedFileBase file)


in this file variable,you'll get all information related to the uploaded file save its path and name in DB
 
Share this answer
 
v2
Comments
adee.sidz 15-Feb-12 5:47am    
That was i wanted :)
aspx:
HTML
<![CDATA[<% Using (Html.BeginForm("ContentUpload", "ContentUpload", FormMethod.Post, New With {.enctype = "multipart/form-data", .onsubmit = "return confirmSubmit();"}))%>]]>

 <input type="file" name="ExcleFile" id="ExcleFile" value="Upload" /> 
 <%End Using %>


Controller:
VB
'ContentUpload controller, ContentUpload action, Post
<acceptverbs(httpverbs.post)> _
    <validateinput(false)> _
    <actionname("contentupload")> _
    Function ContentUpload_Post() As ActionResult
     'Get the file by name tag, not the id tag
      Dim pf As HttpPostedFileBase = Request.Files("ExcleFile")
      Dim filename As String = Path.GetFileName(pf.FileName)
      ' you can get more information about the upload file use HttpPostedFileBase method. 
      'Save to db
      Return View()
    End Function
 
Share this answer
 
Hi as aboce suggested please use form tag with encreption
C#
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))


for more detail check the beow link

http://www.dotnetpools.com/2012/10/file-upload-in-mvc3-by-using-razor.html[^]
 
Share this answer
 
v2
Comments
Sami Ciit 3-Oct-13 16:16pm    
This link is not working http://www.dotnetpools.com/Article/ArticleDetiail/?articleId=44&title=File Upload in MVC3 By Using Razor
[no name] 10-Oct-13 3:01am    
Hi,
Sorry buddy the url have been changed. have a look of this link

http://www.dotnetpools.com/2012/10/file-upload-in-mvc3-by-using-razor.html

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900