我想在页面加载时自动选择文件到FileUpload1。我试过搜索,但我找不到任何相关的实例。我想做这样的事情。
Private Sub WebForm1_Load(sender As Object, e As EventArgs) Handles Me.Load
FileUpload1.setfile("D:\file\Test.Wav")
End Sub
因为我的程序是录制声音(wav文件)并保存到特定文件夹。然后,我可以选择录制的文件并将文件上传/保存到数据库。 单击button_1时,它会使用麦克风录制声音。然后你将按钮_2停止并保存录制的声音。 但我想要做的是点击button_2后,它将停止并保存录音,并异步将文件上传到数据库。
以下是代码:.VB文件
Imports System.Data.SqlClient
Imports System.IO
Imports Microsoft.VisualBasic.Devices
Imports Microsoft.VisualBasic
Imports System.Runtime.InteropServices
Imports System.Text
Partial Class Default2
Inherits System.Web.UI.Page
Protected Sub btnUpload_Click(sender As Object, e As EventArgs) Handles btnUpload.Click
Using br As New BinaryReader(FileUpload1.PostedFile.InputStream)
Dim bytes As Byte() = br.ReadBytes(CInt(FileUpload1.PostedFile.InputStream.Length))
Dim strConnString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(strConnString)
Using cmd As New SqlCommand()
cmd.CommandText = "insert into tblFiles(Name, ContentType, Data) values (@Name, @ContentType, @Data)"
cmd.Parameters.AddWithValue("@Name", Path.GetFileName(FileUpload1.PostedFile.FileName))
cmd.Parameters.AddWithValue("@ContentType", "audio/mpeg3")
cmd.Parameters.AddWithValue("@Data", bytes)
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Using
Response.Redirect(Request.Url.AbsoluteUri)
End Sub
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim strConnString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(strConnString)
Using cmd As New SqlCommand()
cmd.CommandText = "select Id, Name from tblFiles"
cmd.Connection = con
con.Open()
GridView1.DataSource = cmd.ExecuteReader()
GridView1.DataBind()
con.Close()
End Using
End Using
End Sub
<DllImport("winmm.dll")> _
Private Shared Function mciSendString(ByVal command As String, ByVal buffer As StringBuilder, ByVal bufferSize As Integer, ByVal hwndCallback As IntPtr) As Integer
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim i As Integer
i = mciSendString("open new type waveaudio alias capture", Nothing, 0, 0)
i = mciSendString("record capture", Nothing, 0, 0)
Label1.Text = "Recording"
Label1.BackColor = Drawing.Color.Green
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Dim i As Integer
i = mciSendString("save capture " & "D:\file\Test.Wav", Nothing, 0, 0)
i = mciSendString("close capture", Nothing, 0, 0)
Label1.Text = "Idle"
Label1.BackColor = Drawing.Color.Yellow
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub WebForm1_Load(sender As Object, e As EventArgs) Handles Me.Load
End Sub
End Class
aspx.file
<form runat="server">
<asp:Button ID="Button1" runat="server" Text="Record" />
<asp:Button ID="Button2" runat="server" Text="Save" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload"
onclick="btnUpload_Click" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" RowStyle- BackColor="#A1DCF2" Font-Names = "Arial" Font-Size = "10pt"
HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White">
<Columns>
<asp:BoundField DataField="Name" HeaderText="FileName" />
<asp:TemplateField>
<ItemTemplate>
<object type="application/x-shockwave-flash" data='dewplayer-vol.swf?mp3=File.ashx?Id=<%# Eval("Id") %>'
width="240" height="20" id="dewplayer">
<param name="wmode" value="transparent" />
<param name="movie" value='dewplayer-vol.swf?mp3=File.ashx?Id=<%# Eval("Id") %>'/>
<object>
</ItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField DataNavigateUrlFields="Id" Text = "Download" DataNavigateUrlFormatString = "~/File.ashx?Id={0}" HeaderText="Download" />
</Columns>
</asp:GridView>
答案 0 :(得分:0)
由于安全原因,我认为不可能。想象一下,您访问一个随机网站,它会在您不知情/未经许可的情况下自动开始从您的本地文件系统上传文件。
输入类型文件控件要求用户选择要上载的文件,因此无法以编程方式执行此操作。