将视频上传到指定文件夹并将其文件路径保存到数据库中 [英] uploading video to a specifique folder and save his filepath into database

查看:165
本文介绍了将视频上传到指定文件夹并将其文件路径保存到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Private Sub video()
        If IsPostBack Then
            Dim path As String = Server.MapPath("~/UploadedVideos/")
            Dim fileOK As Boolean = False
            If FileUpload1.HasFile Then
                Dim fileExtension As String
                fileExtension = System.IO.Path. _
                GetExtension(FileUpload1.FileName).ToLower()
                Dim allowedExtensions As String() = _
                {".mov", ".wmv", ".avi", ".vob", ".mp4"}
                For i As Integer = 0 To allowedExtensions.Length - 1
                    If fileExtension = allowedExtensions(i) Then
                        fileOK = True
                    End If
                Next
                If fileOK Then
                    Dim Conn As New SqlConnection(ConfigurationManager.ConnectionStrings("projectConnectionString").ConnectionString)

                    Conn.Open()
                    Try
                        Dim FilePath = path & FileUpload1.FileName
                        'Here you can fire your insert statement in filepath value u can pass directly that value
                        Dim SQL As String = "INSERT INTO EmpData ( [Video]) VALUES (@VideoUrl)"
                        Dim cmd As New SqlCommand(SQL, Conn)
                        cmd.Parameters.Add("@VideoUrl", SqlDbType.NVarChar, 50, FilePath)
                        FileUpload1.PostedFile.SaveAs(path & _
                        FileUpload1.FileName)
                        lblError.Text = "File uploaded!"
                        cmd.ExecuteNonQuery()
                        Conn.Close()
                    Catch ex As Exception
                        lblError.Text = "File could not be uploaded." & ex.Message
                    End Try

                Else
                    lblError.Text = "Cannot accept files of this type."
                End If
            End If
        End If
    End Sub




这是我的代码,用于将视频文件上传到Webform应用程序项目中的特定文件夹,但是问题是当我从保存按钮调用该video()时,此错误在浏览器中显示:T
重置连接后,页面加载时重置了与服务器的连接.
这是保存按钮的代码,我在其中调用了video()





this was my code for the uploading a video file to a specific folder in my webform application project but the problem is when i call that video() from the save button this error show in the browser :T
he connection was reset the connection to the server was reset while the page was loading.
this is the code for my save button where i call the video()


Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim connection As SqlConnection = Nothing
        Try
            Dim img As FileUpload = CType(PicUpload, FileUpload)
            Dim imgByte As Byte() = Nothing
            If img.HasFile AndAlso Not img.PostedFile Is Nothing Then
                'To create a PostedFile
                Dim File As HttpPostedFile = PicUpload.PostedFile
                'Create byte Array with file len
                imgByte = New Byte(File.ContentLength - 1) {}
                'force the control to load data in array
                File.InputStream.Read(imgByte, 0, File.ContentLength)
            End If
            ' Insert the employee name and image into db
            Dim conn As String = ConfigurationManager.ConnectionStrings("projectConnectionString").ConnectionString
            connection = New SqlConnection(conn)

            connection.Open()
            Dim sql As String = "INSERT INTO EmpData (DateOfUpload, DescOfWork,WorkDone,ComOfEmp,Picture,EmpUsername)VALUES (@en1m,@en2m,@en3m,@en4m,@eimg,@en5m)SELECT @@IDENTITY"
            Dim cmd As SqlCommand = New SqlCommand(sql, connection)
            cmd.Parameters.AddWithValue("@en1m", Calendar1.SelectedDate)
            cmd.Parameters.AddWithValue("@en2m", TypeOfWork.Text.Trim())
            cmd.Parameters.AddWithValue("@en3m", WorkOfTheDay.Text.Trim())
            cmd.Parameters.AddWithValue("@en4m", ComOfEmp.Text.Trim())
            cmd.Parameters.AddWithValue("@eimg", imgByte)
            cmd.Parameters.AddWithValue("@en5m", Request.QueryString("id"))
            Dim id As Integer = Convert.ToInt32(cmd.ExecuteScalar())


        Catch ex As Exception
            MsgBox(ex.Message)

        Finally
            connection.Close()
        End Try
        Call video()
        Response.Redirect("Employee.aspx?id=" + TextBox1.Text, False)
    End Sub



plz,如果sm1可以帮助我,我会很感激,谢谢您



plz if sm1 can help me out i will appreciate it thank you in advanced

推荐答案

查找< httpruntime maxrequestlength ="2097151" enable ="true">在web.config中(如果有).否则在
中添加 < system.web>
.
.


Find <httpruntime maxrequestlength="2097151" enable="true"> in web.config if you have.Otherwise Add in
<system.web>
.
.

as
system.web>
   <!--
           Set compilation debug="true" to insert debugging
           symbols into the compiled page. Because this
           affects performance, set this value to true only
           during development.
       -->
   <compilation debug="true">
     <assemblies>
       <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
       <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
       <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
       <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
       <add assembly="System.Web.Extensions.Design, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
       <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
       <add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
       </assemblies>
     <buildProviders>
       <add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.Common, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
     </buildProviders>
   </compilation>
   <!--
           The <authentication> section enables configuration
           of the security authentication mode used by
           ASP.NET to identify an incoming user.
       -->
   <authentication mode="Windows" />
   <!--<httpRuntime executionTimeout="1900" maxRequestLength="2090000" />-->

   <httpRuntime maxRequestLength="2097151" enable="true" />
<customErrors mode="Off" defaultRedirect="GenericErrorPage.htm">
           <error statusCode="403" redirect="NoAccess.htm" />
           <error statusCode="404" redirect="underconstruction.htm" />
         <error statusCode="500" redirect="underconstruction.htm"/>
       </customErrors>

   <pages enableEventValidation="false">
     <controls>
       <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
       <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
     </controls>
   </pages>
   <httpHandlers>
     <remove path="*.asmx" verb="*" />
     <add path="*.asmx" verb="*" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false" />
     <add path="*_AppService.axd" verb="*" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false" />
     <add path="ScriptResource.axd" verb="GET,HEAD" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false" />
     <add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="false" />
   </httpHandlers>
   <httpModules>
     <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
     <add name="OnlineActiveUsers" type="OnlineActiveUsers.OnlineUsersModule" />
   </httpModules>
 </system.web>



在web.config中



in web.config


Private Sub video()
       Dim connection As SqlConnection = Nothing
       Dim img As FileUpload = CType(PicUpload, FileUpload)
       Dim imgByte As Byte() = Nothing
       If img.HasFile AndAlso Not img.PostedFile Is Nothing Then
           'To create a PostedFile
           Dim File As HttpPostedFile = PicUpload.PostedFile
           'Create byte Array with file len
           imgByte = New Byte(File.ContentLength - 1) {}
           'force the control to load data in array
           File.InputStream.Read(imgByte, 0, File.ContentLength)
       End If
       If IsPostBack Then
           'declare the file path to store uploads
           Dim path As String = Server.MapPath("~/UploadedVideos/")


           Dim fileOK As Boolean = False

           'check the file to make sure it is of the required type
           Dim fileExtension As String
           fileExtension = System.IO.Path. _
               GetExtension(FileUpload1.FileName).ToLower()
           'declare the allowed extentions
           Dim allowedExtensions As String() = _
               {".mov", ".wmv", ".avi", ".mp4", ".m4v", ".flv"}
           'loop to check the extention type
           For i As Integer = 0 To allowedExtensions.Length - 1
               If fileExtension = allowedExtensions(i) Then

                   fileOK = True

               End If



           Next


           '******************* Write Video Immage And Details into the Database *******************************

           Dim conn As String = ConfigurationManager.ConnectionStrings("projectConnectionString").ConnectionString


           Try
               FileUpload1.FileName.DefaultIfEmpty()
               Dim FilePath = path & FileUpload1.FileName

               connection = New SqlConnection(conn)

               connection.Open()

               Dim sql As String = "INSERT INTO EmpData (DateOfUpload, DescOfWork,WorkDone,ComOfEmp,Picture,EmpUsername,Video)VALUES (@en1m,@en2m,@en3m,@en4m,@eimg,@en5m,@VideoUrl)SELECT @@IDENTITY"
               Dim cmd As SqlCommand = New SqlCommand(sql, connection)

               cmd.Parameters.AddWithValue("@en1m", Calendar1.SelectedDate)
               cmd.Parameters.AddWithValue("@en2m", TypeOfWork.Text.Trim())
               cmd.Parameters.AddWithValue("@en3m", WorkOfTheDay.Text.Trim())
               cmd.Parameters.AddWithValue("@en4m", ComOfEmp.Text.Trim())
               cmd.Parameters.AddWithValue("@eimg", imgByte)
               cmd.Parameters.AddWithValue("@en5m", Request.QueryString("id"))
               If fileOK Then
                   cmd.Parameters.AddWithValue("@VideoUrl", FilePath)
               Else
                   cmd.Parameters.AddWithValue("@VideoUrl", DBNull.Value)
               End If

               Dim id As Integer = Convert.ToInt32(cmd.ExecuteScalar())
               FileUpload1.PostedFile.SaveAs(path & _
                  FileUpload1.FileName)


           Catch ex As Exception
               MsgBox(ex.Message)

           End Try

       End If





   End Sub


这篇关于将视频上传到指定文件夹并将其文件路径保存到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆