如何在单击按钮上保存多个上传的文件

时间:2013-12-23 11:54:49

标签: asp.net vb.net visual-studio-2010

我有一个gridview,在一列中有fileupload选项,我一次只能保存一个图像文件,但如何保存上传到gridview不同行的多个图像? 我的设计页面:

<div>
     <br/>
       <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>     
     <br/>                
         <asp:Label ID="Label1" runat="server" Font-Bold="True" ForeColor="#000099"></asp:Label>
      <br/>
           <asp:Button ID="button1" Text="Upload"  runat="server" Width="73px"  onclick="button1_Click" />
        <br/>

<asp:GridView ID="grdimagepanel" runat="server" Width="100%" CssClass="GridBooking"
                                                    AutoGenerateColumns="False">
                                                    <Columns>
                                                         <asp:TemplateField HeaderText="File Path">
                                                             <ItemTemplate>
                                                                <asp:FileUpload  ID="fileupload1" runat="server" />

                                                            </ItemTemplate>
                                                              </asp:TemplateField>
                                                     <asp:TemplateField HeaderText="Image Descriptions">
                                                            <ItemTemplate>
                                                                <asp:TextBox ID="txtimagedescription" runat="server" 

                                                                    CssClass="txtgrid1" Width="98%" ></asp:TextBox>
                                                            </ItemTemplate>
                                                            <HeaderStyle Width="75%" />
                                                        </asp:TemplateField>
                                                        <asp:TemplateField HeaderText="Scanned">
                                                            <ItemTemplate>
                                                                <table>
                                                                    <tr>
                                                                        <td>
                                                                            <asp:TextBox ID="txtimagedate" runat="server" 

                                                                                 Width="100px" CssClass="txtgrid" ></asp:TextBox>
                                                                        </td>
                                                                        <td>
                                                                            <asp:ImageButton ID="imgbtnScanDatetime"  runat="server"
                                                                                CssClass="btncalnder" />
                                                                           <%-- <asp:CalendarExtender ID="CalendarExtender4" runat="server" 
                                                                                Format="MM-dd-yyyy" TargetControlID="txtimagedate">
                                                                            </asp:CalendarExtender>--%>
                                                                        </td>
                                                                    </tr>
                                                                </table>
                                                            </ItemTemplate>
                                                        </asp:TemplateField>
                                                        <asp:TemplateField HeaderText=" ">
                                                            <ItemTemplate>
                                                                <asp:ImageButton ID="ImgbtnImageTab" runat="server" 
                                                                      Width="10"
                                                                    Height="20" />

                                                            </ItemTemplate>
                                                        </asp:TemplateField>
                                                    </Columns>
                                                </asp:GridView>
</div>

我的代码页面背后:

Protected Sub button1_Click(sender As Object, e As EventArgs)
    For Each grdRow As GridViewRow In grdimagepanel.Rows
        Dim fileupload1 As FileUpload = TryCast(Me.grdimagepanel.Rows(ViewState("SelectedRowIndex")).FindControl("fileupload1"), FileUpload)

        'Dim fileupload1 As FileUpload = grdimagepanel.FindControl("fileupload1")
        If fileupload1.HasFile Then
            Try

                If fileupload1.PostedFile.ContentType = "image/jpeg" Then
                    If fileupload1.PostedFile.ContentLength < 5120000 Then
                        Dim filename As String = Path.GetFileName(fileupload1.FileName)
                        fileupload1.SaveAs(Server.MapPath("~/F:\Abc\ImageUploaded\") & filename)
                        Label1.Text = "File uploaded successfully!"
                        Label2.Text = Server.MapPath(fileupload1.FileName)
                    Else

                        Label1.Text = "File maximum size is 500 Kb"
                    End If
                Else

                    Label1.Text = "Only JPEG files are accepted!"
                End If
            Catch exc As Exception

                Label1.Text = "The file could not be uploaded. The following error occured: " & exc.Message
            End Try
        End If
    Next
End Sub

1 个答案:

答案 0 :(得分:1)

请尝试以下代码:

你已经有网格行,所以你需要找到fileuplaod控件grdRow而不是使用SelectedRowIndex

Protected Sub button1_Click(sender As Object, e As EventArgs)
    For Each grdRow As GridViewRow In grdimagepanel.Rows
        Dim fileupload1 As FileUpload = TryCast(grdRow.FindControl("fileupload1"), FileUpload)
        'Dim fileupload1 As FileUpload = grdimagepanel.FindControl("fileupload1")
        If fileupload1.HasFile Then
            Try

                If fileupload1.PostedFile.ContentType = "image/jpeg" Then
                    If fileupload1.PostedFile.ContentLength < 5120000 Then
                        Dim filename As String = Path.GetFileName(fileupload1.FileName)
                        fileupload1.SaveAs(Server.MapPath("~/F:\ImageUploaded\") & filename)
                        Label1.Text = "File uploaded successfully!"
                        Label2.Text = Server.MapPath(fileupload1.FileName)
                    Else

                        Label1.Text = "File maximum size is 500 Kb"
                    End If
                Else

                    Label1.Text = "Only JPEG files are accepted!"
                End If
            Catch exc As Exception

                Label1.Text = "The file could not be uploaded. The following error occured: " & exc.Message
            End Try
        End If
    Next
End Sub