Click here to Skip to main content
12,943,742 members (56,071 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 -
<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 14-Feb-12 1:01am
Updated 14-Feb-12 1:16am
RaisKazi34.6K
v2
Rate this: bad
 
good
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
  Permalink  
v2
Comments
adee.sidz 15-Feb-12 5:47am
   
That was i wanted :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

aspx:
<![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:
'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
  Permalink  
Rate this: bad
 
good
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

http://www.dotnetpools.com/2012/10/file-upload-in-mvc3-by-using-razor.html[^]
  Permalink  
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
Member 3823101 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)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,643
CHill60 2,970
Maciej Los 2,348
Jochen Arndt 1,900
ppolymorphe 1,765


Advertise | Privacy | Mobile
Web02 | 2.8.170518.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