Asp.net 如何在Gridview中选择每行中的文件后自动上载图像

Asp.net 如何在Gridview中选择每行中的文件后自动上载图像,asp.net,vb.net,Asp.net,Vb.net,我有一个gridview,每行都有一个文件上传。当您单击文件上载时,它会选择该文件,然后我在gridview行中的按钮上单击上载 我想消除点击上传按钮,只是自动上传文件后浏览它 我该怎么做 以下是我当前运行的gridview和代码: <asp:GridView ID="gvParts" runat="server" class="table table-bordered table-highlight" AutoGenerateColumns="False" DataSourceID="S

我有一个gridview,每行都有一个文件上传。当您单击文件上载时,它会选择该文件,然后我在gridview行中的按钮上单击上载

我想消除点击上传按钮,只是自动上传文件后浏览它

我该怎么做

以下是我当前运行的gridview和代码:

<asp:GridView ID="gvParts" runat="server" class="table table-bordered table-highlight" AutoGenerateColumns="False" DataSourceID="SqlDataSource2">
                      <Columns>
                          <asp:TemplateField HeaderText="Action">
                              <ItemTemplate>
                                  <center><asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/img/imageicon.png" CommandName="View" Visible="False" /></center>
                                             <asp:FileUpload ID="FileUpload1" runat="server" Width="104px" />
                        <asp:Button ID="Button1" runat="server" CommandArgument='<%# Container.DataItemIndex  %>'
                            Text="Upload" OnClick="Button1_Click" />
                              </ItemTemplate>
                              <ItemStyle Width="100px" />
                          </asp:TemplateField>
                          <asp:BoundField DataField="PART_ID" HeaderText="Part ID" SortExpression="PART_ID" />
                          <asp:BoundField HeaderText="Has Image" />
                          <asp:BoundField DataField="PART_NUMBER" HeaderText="Part Number" SortExpression="PART_NUMBER" />
                          <asp:BoundField DataField="DESCRIPTION" HeaderText="Description" SortExpression="DESCRIPTION" />
                          <asp:BoundField DataField="DESCRIPTION2" HeaderText="Description 2" SortExpression="DESCRIPTION2" />
                      </Columns>
                  </asp:GridView>

这是我在点击上传按钮后要执行的代码(我正试图消除这个按钮,所以我只需选择文件,它就会自动上传)

受保护的子按钮1\u单击(ByVal sender作为对象,ByVal e作为事件参数)
对于gvParts.Rows中作为GridViewRow的每一行
作为FileUpload=CType的Dim文件(row.FindControl(“FileUpload1”),FileUpload)
如果(Not(file)为空),则
如果file.HasFile那么
'读取文件并将其转换为字节数组
Dim filePath为String=file.PostedFile.FileName
Dim文件名为String=Path.GetFileName(filePath)
Dim ext As String=Path.GetExtension(文件名)
Dim contenttype为String=String.Empty
'根据文件扩展名设置contenttype
选择案例分机
案例“.jpg”
contenttype=“image/jpg”
退出选择
大小写“.png”
contenttype=“image/png”
退出选择
大小写“.gif”
contenttype=“image/gif”
退出选择
结束选择
如果contenttype为String.Empty,则
Dim fs As Stream=file.PostedFile.InputStream
Dim br作为新的二进制读取器(fs)
作为字节()的Dim字节=br.ReadBytes(fs.Length)
'将文件插入数据库
Dim strQuery As String=“插入tbl_TMGPartImages(PartID、ImageName、ContentType、Data)值(@PartID、@ImageName、@ContentType、@Data)”
Dim cmd作为新的SqlCommand(strQuery)
cmd.Parameters.AddWithValue(“@PartID”,行.单元格(1.Text)
cmd.Parameters.Add(“@ImageName”,SqlDbType.VarChar).Value=filename
cmd.Parameters.Add(“@ContentType”,SqlDbType.VarChar).Value_
=内容类型
cmd.Parameters.Add(“@Data”,SqlDbType.Binary).Value=bytes
尝试
Dim con8作为SqlClient.SqlConnection
Dim cmd8作为SqlClient.SqlCommand
con8=新的SqlClient.SqlConnection(System.Configuration.ConfigurationManager.AppSettings(“MainConnectionString”).ToString)
con8.Open()
cmd8=新建SqlClient.SqlCommand
cmd8=新的SqlCommand(“从[TMGGoods].[dbo].[tbl_TMGPartImages]中删除,其中PartID=@PartID”,con8)
cmd8.Parameters.AddWithValue(“@PartID”,行.单元格(1.Text)
cmd8.ExecuteNonQuery()
con8.Close()
特例
结束尝试
插入更新数据(cmd)
其他的
如果结束
如果结束
如果结束
下一个
端接头

如果你想在上传前查看,请查看@Abdul的链接,这不是我想要做的。
   Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        For Each row As GridViewRow In gvParts.Rows
            Dim file As FileUpload = CType(row.FindControl("FileUpload1"), FileUpload)
            If (Not (file) Is Nothing) Then
                If file.HasFile Then

                    ' Read the file and convert it to Byte Array
                    Dim filePath As String = file.PostedFile.FileName
                    Dim filename As String = Path.GetFileName(filePath)
                    Dim ext As String = Path.GetExtension(filename)
                    Dim contenttype As String = String.Empty

                    'Set the contenttype based on File Extension
                    Select Case ext
                        Case ".jpg"
                            contenttype = "image/jpg"
                            Exit Select
                        Case ".png"
                            contenttype = "image/png"
                            Exit Select
                        Case ".gif"
                            contenttype = "image/gif"
                            Exit Select
                    End Select
                    If contenttype <> String.Empty Then
                        Dim fs As Stream = file.PostedFile.InputStream
                        Dim br As New BinaryReader(fs)
                        Dim bytes As Byte() = br.ReadBytes(fs.Length)

                        'insert the file into database
                        Dim strQuery As String = "insert into tbl_TMGPartImages (PartID, ImageName, ContentType, Data) values (@PartID, @ImageName, @ContentType, @Data)"
                        Dim cmd As New SqlCommand(strQuery)
                        cmd.Parameters.AddWithValue("@PartID", row.Cells(1).Text)
                        cmd.Parameters.Add("@ImageName", SqlDbType.VarChar).Value = filename
                        cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value _
                        = contenttype
                        cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes

                        Try
                            Dim con8 As SqlClient.SqlConnection
                            Dim cmd8 As SqlClient.SqlCommand
                            con8 = New SqlClient.SqlConnection(System.Configuration.ConfigurationManager.AppSettings("MainConnectionString").ToString)
                            con8.Open()
                            cmd8 = New SqlClient.SqlCommand
                            cmd8 = New SqlCommand("Delete from [TMGGoods].[dbo].[tbl_TMGPartImages] where PartID = @PartID", con8)
                            cmd8.Parameters.AddWithValue("@PartID", row.Cells(1).Text)
                            cmd8.ExecuteNonQuery()
                            con8.Close()

                        Catch ex As Exception

                        End Try

                        InsertUpdateData(cmd)

                    Else

                    End If


                End If

            End If

        Next
    End Sub