Click here to Skip to main content
13,145,068 members (54,560 online)
Rate this:
Please Sign up or sign in to 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 -
       <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>())

When I click on upload button it will save in DB as I need.
Posted 14-Feb-12 1:01am
Updated 14-Feb-12 1:16am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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

and in you Actionresult pass a parameter

Actionresult(HttpPostedFileBase file)

in this file variable,you'll get all information related to the uploaded file save its path and name in DB
adee.sidz 15-Feb-12 5:47am
That was i wanted :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

<![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 %>

'ContentUpload controller, ContentUpload action, Post
<acceptverbs(> _
    <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
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Hi as aboce suggested please use form tag with encreption
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))

for more detail check the beow link[^]
Sami Ciit 3-Oct-13 16:16pm
This link is not working Upload in MVC3 By Using Razor
Member 3823101 10-Oct-13 3:01am
Sorry buddy the url have been changed. have a look of this link

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170915.1 | Last Updated 28 Nov 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100