{"id":1771,"date":"2011-12-15T17:55:46","date_gmt":"2011-12-15T21:55:46","guid":{"rendered":"http:\/\/www.iowawebnet.com\/ein\/?p=1771"},"modified":"2011-12-21T16:05:21","modified_gmt":"2011-12-21T20:05:21","slug":"vb-net-single-file-upload","status":"publish","type":"post","link":"https:\/\/www.iowawebnet.com\/ein\/2011\/12\/vb-net-single-file-upload\/","title":{"rendered":"VB.NET &#8211; Single file upload"},"content":{"rendered":"<p>WUI View<br \/>\n<body><\/p>\n<div style=\"font-size: .80em; font-family: Verdana, Arial, Helvetica, Verdana, sans-serif;\">\n<a href=\"\">Home<\/a> | <a href=\"\">Upload File<\/a> | <a href=\"\">Close Window<\/a><\/p>\n<fieldset>\n<legend>Enter requested information:<\/legend>\n<form id=\"Form2\" runat=\"server\" enctype=\"multipart\/form-data\">\n<ul style=\"list-style-type: none; line-height: 22px;\">\n<li>Your name<br \/><input type=\"textbox\" value=\"\"\/> <font color=\"red\">X<\/font><\/li>\n<li>File title:<br \/><input type=\"textbox\" value=\"Inglourious Basterds\"\/><\/li>\n<li>File category:<br \/>\n         <select><option>Comedy<\/option><option>Please select&#8230;<\/option><option>Sci-Fi<\/option><option>Wild West<\/option><\/select><\/li>\n<li>File type:<br \/>\n         <select><option>DivX Video File<\/option><option>Please select&#8230;<\/option><option>Windows movie video<\/option><option>Audio Video Interleave File<\/option><\/select> <\/li>\n<li>Select a file to upload:<br \/><input type=\"file\" id=\"File1\" \/><\/li>\n<li>Additional note:<br \/><textarea><\/textarea>\n     <\/li>\n<li><asp:label ID=\"MsgLabel\" runat=\"server\" class=\"error_lbl\"><\/asp:label> <span id=\"Span1\" runat=\"Server\" \/><\/li>\n<li>\n        <input type=\"submit\" value=\"Upload File\" disabled\/><\/li>\n<\/ul><\/form>\n<\/fieldset><\/div>\n<p><\/body><\/p>\n<pre>\r\n<a style=\"display:none;\" id=\"te1212223328\" href=\"javascript:expand('#te1212223328')\"><strong>Designer view (expand)<\/strong><\/a>\n<div class=\"te_div\" id=\"te1212223328\"><script language=\"JavaScript\" type=\"text\/javascript\">expander_hide('#te1212223328');<\/script>\r\n<code>\r\n&lt; %@ Import Namespace=\"System.IO\" %>\r\n&lt; %@ Import Namespace=\"System.Data.SqlClient\" %>\r\n&lt; %@ Page Language=\"VB\" %>\r\n\r\n&lt; !DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.0 \r\nTransitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd\">\r\n\r\n&lt;script runat=\"server\">\r\nSub SubmitButton_Click(Source As Object, e As EventArgs)\r\n   'defining controls\r\n    Dim UName As String\r\n    Dim FTitle As String\r\n    Dim FType As String\r\n    Dim FCat As String\r\n    Dim uComment As String\r\n    Dim uDate As DateTime\r\n        \r\n   'binding controls and converting them to strings\r\n    UName = UsrName.Text.ToString\r\n    FTitle = FileTitle.Text.ToString\r\n    FType = FileType.Text.ToString\r\n    FCat = FileCat.Text.ToString\r\n    uComment = txtCmt.Text.ToString\r\n    uDate = DateAndTime.Now.ToString\r\n        \r\nIf Not (File1.PostedFile Is Nothing) Then\r\n            \r\n  'create db connection string\r\n   Dim SQLString As String\r\n   Dim ConnString As String\r\n            \r\n   ConnString = \"Data Source=my-srv-mssql;Initial Catalog=intranet;\r\n                 Persist Security Info=True;User ID=usr;Password=pwd\"\r\n            \r\n  Try\r\n   If FType = \"wmv\" Then\r\n      File1.PostedFile.SaveAs(\"C:\\inetpub\\wwwwroot\\my_website\\doc_vault\\\" &_ \r\n                                                                FTitle + \".\" + FType)\r\n\r\n      Span1.InnerHtml = \"&lt;span class='positive_conf'>Upload Successful - \r\n                        &lt;a href='Get-File.aspx'>Upload another file!&lt;\/a>&lt;\/span>\"\r\n   Else\r\n     'example of user alert just in case if decide to stick with certain type of file\r\n      MsgLabel.Text = \"It looks like you are trying to upload the video, but you didn't \r\n                       specify wmv file type!\"\r\n   End If\r\n     'SQL statement\r\n      SQLString = \"INSERT into UplTbl(wUser, fTitle, fType, fCat, fComment, uDate) &_\r\n                   VALUES (@uName, @FTitle, @FType, @FCat, @uComment, @uDAte)\"\r\n                \r\n     'write to SQL\r\n      Dim SQLConn As New SqlConnection()\r\n      Dim SQLCmd As New SqlCommand()\r\n                \r\n      SQLConn.ConnectionString = ConnString\r\n      SQLConn.Open()  'open SQL connection\r\n                \r\n      SQLCmd.Parameters.AddWithValue(\"@uName\", UName)\r\n      SQLCmd.Parameters.AddWithValue(\"@FTitle\", FTitle)\r\n      SQLCmd.Parameters.AddWithValue(\"@FType\", FType)\r\n      SQLCmd.Parameters.AddWithValue(\"@FCat\", FCat)\r\n      SQLCmd.Parameters.AddWithValue(\"@uComment\", uComment)\r\n      SQLCmd.Parameters.AddWithValue(\"@uDate\", uDate)\r\n      SQLCmd.Connection = SQLConn\r\n      SQLCmd.CommandText = SQLString\r\n      SQLCmd.ExecuteNonQuery()  'execute SQL connection\r\n      SQLConn.Close()   'close SQL connection\r\n                \r\n      Catch ex As Exception\r\n      Span1.InnerHtml = \"Error saving file &lt;b>C:\\\\\" & _\r\n                   File1.Value & \"&lt;\/b><br \/>\" & ex.ToString()\r\n   End Try\r\nEnd If\r\nEnd Sub\r\n&lt;\/script>\r\n\r\n&lt;html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\r\n&lt;head runat=\"server\">\r\n  &lt;title>File upload\r\n  &lt;link href=\"Styles\/Site.css\" rel=\"stylesheet\" type=\"text\/css\" \/>\r\n&lt;\/head>\r\n&lt;body>\r\n    &lt;div class=\"page\">\r\n      &lt;a href=\"Default.aspx\">Home&lt;\/a> | \r\n      &lt;a href=\"Upload.aspx\">Upload File&lt;\/a> | \r\n      &lt;a href=\"\">Close Window&lt;\/a>\r\n    &lt;fieldset>\r\n      &lt;legend>Enter requested information:\r\n      &lt;form id=\"Form2\" runat=\"server\" enctype=\"multipart\/form-data\">\r\n      &lt;ul class=\"upload_ul\">\r\n      &lt;li>Your name&lt;br \/>\r\n      &lt;asp:textbox ID=\"UsrName\" runat=\"server\" ToolTip=\"Your name\" class=\"txtbox_text\">\r\n                                                                         &lt;\/asp:textbox>\r\n      &lt;asp:requiredfieldvalidator id=\"name\" runat=\"server\" ErrorMessage=\"X\" \r\n                                                  ControlToValidate=\"UsrName\"> \r\n      &lt;\/asp:requiredfieldvalidator>\r\n      &lt;\/li>\r\n      &lt;li>File title:&lt;br \/>\r\n      &lt;asp:textbox ID=\"FileTitle\" runat=\"server\" ToolTip=\"File title\" \r\n                                         class=\"txtbox_text\">&lt;\/asp:textbox>\r\n      &lt;asp:requiredfieldvalidator id=\"title\" runat=\"server\" ErrorMessage=\"X\" \r\n                                                 ControlToValidate=\"FileTitle\"> \r\n      &lt;\/asp:requiredfieldvalidator>\r\n      &lt;\/li>\r\n      &lt;li>File category:&lt;br \/>&lt;asp:dropdownlist ID=\"FileCat\" runat=\"server\" \r\n                                                                class=\"txtbox_text\">\r\n        &lt;asp:listitem Value=\"\" Text=\"Please select\" Selected>&lt;\/asp:listitem>\r\n        &lt;asp:listitem Value=\"SF\" Text=\"Sci-Fi\">&lt;\/asp:listitem>\r\n        &lt;asp:listitem Value=\"WW\" Text=\"Wild West\">&lt;\/asp:listitem>\r\n        &lt;<asp:listitem Value=\"CM\" Text=\"Comedy\">&lt;\/asp:listitem>\r\n      &lt;\/asp:dropdownlist>\r\n      &lt;asp:requiredfieldvalidator id=\"category\" runat=\"server\" ErrorMessage=\"X\" \r\n                                                      ControlToValidate=\"FileCat\"> \r\n      &lt;\/li>\r\n      &lt;li>File type:&lt;br \/>\r\n      &lt;asp:dropdownlist ID=\"FileType\" runat=\"server\" class=\"txtbox_text\">\r\n        &lt;asp:listitem Value=\"\" Text=\"Please select\" Selected>&lt;\/asp:listitem>\r\n        &lt;<\/asp:listitem><asp:listitem Value=\"wmv\" Text=\"Windows movie video\">\r\n                                                              &lt;\/asp:listitem>\r\n        &lt;<\/asp:listitem><asp:listitem Value=\"dvx\" Text=\"DivX Video File\">\r\n                                                              &lt;\/asp:listitem>\r\n        &lt;asp:listitem Value=\"avi\" Text=\"Audio Video Interleave File\">&lt;\/asp:listitem>\r\n      &lt;\/asp:dropdownlist>\r\n      &lt;asp:requiredfieldvalidator id=\"type\" runat=\"server\" ErrorMessage=\"X\" \r\n                ControlToValidate=\"FileType\"> &lt;\/asp:requiredfieldvalidator>\r\n      &lt;\/li>\r\n     &lt;li>Select a file to upload:&lt;br \/>\r\n     &lt;input type=\"file\" id=\"File1\" runat=\"Server\"\/>\r\n     &lt;asp:requiredfieldvalidator id=\"RequiredFieldValidator1\" runat=\"server\" \r\n    ErrorMessage=\"X\" ControlToValidate=\"File1\"> &lt;\/asp:requiredfieldvalidator>\r\n     &lt;\/li>\r\n     &lt;li>Additional note:<br \/>\r\n     &lt;asp:textbox ID=\"txtCmt\" runat=\"server\" ToolTip=\"User comment\" \r\n                      class=\"txtbox_text\" TextMode=\"MultiLine\">&lt;\/asp:textbox>\r\n     &lt;\/li>\r\n     &lt;li>&lt;asp:label ID=\"MsgLabel\" runat=\"server\" class=\"error_lbl\"> <span id=\"Span1\" \r\n                                                    runat=\"Server\" \/>&lt;br \/>&lt;\/li>\r\n     &lt;li>\r\n     &lt;input type=\"submit\" id=\"Submit1\" runat=\"Server\" \r\n         value=\"Upload File\" OnServerClick=\"SubmitButton_Click\"\/>&lt;\/li>\r\n     &lt;\/ul>\r\n    &lt;\/form>\r\n    &lt;\/fieldset>\r\n    &lt;\/div>\r\n&lt;\/body>\r\n&lt;\/html>\r\n<\/asp:listitem><\/code>\r\n<\/div>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>WUI View Home | Upload File | Close Window Enter requested information: Your name X File title: File category: ComedyPlease select&#8230;Sci-FiWild West File type: DivX Video FilePlease select&#8230;Windows movie videoAudio Video Interleave File Select a file to upload: Additional note: Designer view (expand) &lt; %@ Import Namespace=&#8221;System.IO&#8221; %> &lt; %@ Import Namespace=&#8221;System.Data.SqlClient&#8221; %> &lt; %@ [&hellip;]<\/p>\n","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,16,56],"tags":[19,78,79,21],"class_list":["post-1771","post","type-post","status-publish","format-standard","hentry","category-applications","category-net","category-validation-controls","tag-asp-net","tag-file","tag-upload","tag-visual-studio"],"_links":{"self":[{"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/posts\/1771"}],"collection":[{"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/comments?post=1771"}],"version-history":[{"count":62,"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/posts\/1771\/revisions"}],"predecessor-version":[{"id":1836,"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/posts\/1771\/revisions\/1836"}],"wp:attachment":[{"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/media?parent=1771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/categories?post=1771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.iowawebnet.com\/ein\/wp-json\/wp\/v2\/tags?post=1771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}