Click here to Skip to main content
11,715,491 members (85,694 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# MVC3
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
Edited 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 at 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 at 3-Oct-13 16:16pm
This link is not working Upload in MVC3 By Using Razor
Member 3823101 at 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
0 Sergey Alexandrovich Kryukov 683
1 Richard MacCutchan 280
2 Maciej Los 259
3 OriginalGriff 160
4 F-ES Sitecore 135
0 Sergey Alexandrovich Kryukov 1,003
1 Richard MacCutchan 430
2 CHill60 415
3 Maciej Los 399
4 OriginalGriff 385

Advertise | Privacy | Mobile
Web04 | 2.8.150901.1 | Last Updated 28 Nov 2013
Copyright © CodeProject, 1999-2015
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