Asp.net 如何一次点击按钮保存多个上传文件
我有一个gridview,它在一列中有文件上传选项,我一次只能保存一个图像文件,但是如何保存在gridview不同行中上传的多个图像? 我的设计页面:Asp.net 如何一次点击按钮保存多个上传文件,asp.net,vb.net,visual-studio-2010,Asp.net,Vb.net,Visual Studio 2010,我有一个gridview,它在一列中有文件上传选项,我一次只能保存一个图像文件,但是如何保存在gridview不同行中上传的多个图像? 我的设计页面: <div> <br/> <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> <br/> <asp:Label I
<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\u单击(发件人作为对象,e作为事件参数)
对于grdimagepanel.Rows中的每个grdRow作为GridViewRow
Dim fileupload1 As FileUpload=TryCast(Me.grdimagepanel.Rows(ViewState(“SelectedRowIndex”)).FindControl(“fileupload1”),FileUpload)
'Dim fileupload1 As FileUpload=grdimagepanel.FindControl(“fileupload1”)
如果fileupload1.HasFile,则
尝试
如果fileupload1.PostedFile.ContentType=“image/jpeg”,则
如果fileupload1.PostedFile.ContentLength<5120000,则
Dim filename As String=Path.GetFileName(fileupload1.filename)
fileupload1.SaveAs(Server.MapPath(“~/F:\Abc\ImageUploaded\”)和文件名)
Label1.Text=“文件上传成功!”
Label2.Text=Server.MapPath(fileupload1.FileName)
其他的
Label1.Text=“文件最大大小为500 Kb”
如果结束
其他的
Label1.Text=“仅接受JPEG文件!”
如果结束
捕获exc作为例外
Label1.Text=“无法上载文件。出现以下错误:”&exc.Message
结束尝试
如果结束
下一个
端接头
请尝试以下代码:
您已经有了网格行,因此需要查找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
受保护的子按钮1\u单击(发件人作为对象,e作为事件参数)
对于grdimagepanel.Rows中的每个grdRow作为GridViewRow
Dim fileupload1作为FileUpload=TryCast(grdRow.FindControl(“fileupload1”),FileUpload)
'Dim fileupload1 As FileUpload=grdimagepanel.FindControl(“fileupload1”)
如果fileupload1.HasFile,则
尝试
如果fileupload1.PostedFile.ContentType=“image/jpeg”,则
如果fileupload1.PostedFile.ContentLength<5120000,则
Dim filename As String=Path.GetFileName(fileupload1.filename)
fileupload1.SaveAs(Server.MapPath(“~/F:\ImageUploaded\”)和文件名)
Label1.Text=“文件上传成功!”
Label2.Text=Server.MapPath(fileupload1.FileName)
其他的
Label1.Text=“文件最大大小为500 Kb”
如果结束
其他的
Label1.Text=“仅接受JPEG文件!”
如果结束
捕获exc作为例外
Label1.Text=“无法上载文件。出现以下错误:”&exc.Message
结束尝试
如果结束
下一个
端接头
感谢您的建议,但它如何能够上载多个文件