循环浏览 3 张照片并使用递增的名称保存每张照片
Loop through 3 photos and save each one with incremented name
我可以用这样输入的代码让它正确上传所有 1、2 或 3 个文件--
else{
string Path = Server.MapPath("~/IncomingPhotos/" + file.FileName);
file.SaveAs(Path);
}
但我需要能够用这样的名称保存每个文件--
1.1003.IncomingPhoto1
1.1003.IncomingPhoto2
1.1003.IncomingPhoto3
1表示order_id,1003表示customer_id.
表单代码:
<table>
<tr>
<td align="center" class="auto-style2" colspan="2">
<asp:FileUpload ID="PackagingPhoto1" runat="server" Width="437px" AllowMultiple="True" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="packagingPhotos" runat="server" OnClick="packagingPhotos_Click" Text="Upload
Photos" Width="175px" />
</td>
</tr>
</table>
隐藏代码:
protected void packagingPhotos_Click(object sender, EventArgs e)
{
if (PackagingPhoto1.HasFile && PackagingPhoto1.PostedFiles.Count <= 3)
{
foreach (HttpPostedFile file in PackagingPhoto1.PostedFiles)
{
string extension = Path.GetExtension(file.FileName);
if (extension.ToLower() != ".jpeg" && extension.ToLower() != ".jpg" && extension.ToLower() != ".png")
{
Label9.Text = "Only files with .jpg, .jpeg, or .png extension are allowed.";
Label9.ForeColor = System.Drawing.Color.Red;
}
else
{
int photoSize = file.ContentLength;
if (photoSize > 5242880)
{
Label9.Text = "Maximum file size (5MB) exceeded!";
Label9.ForeColor = System.Drawing.Color.Red;
}
else
{
for (int i = 1; i < 3; i++)
{
string Path = Server.MapPath("~/IncomingPhotos/" + GridView1.SelectedRow.Cells[1].Text + "." + GridView1.SelectedRow.Cells[2].Text + ".ReceivingPhoto" + i + extension);
file.SaveAs(Path);
}
Label9.Text = "Photos were successfully uploaded.";
Label9.ForeColor = System.Drawing.Color.Green;
}
}
}
}
else
{
Label9.Text = "Please select at least 1 but no more than 3 files.";
Label9.ForeColor = System.Drawing.Color.Red;
}
}
您的问题是您在错误的位置实例化了计数器变量 (i),并且您有一个不必要的 for 循环。
第 1 步:
在 foreach 循环上方的行中声明并将计数器变量实例化为 0。当你在使用它时,称它为比 "i" 更好的东西,比如 "fileCounter"
if (PackagingPhoto1.HasFile && PackagingPhoto1.PostedFiles.Count <= 3)
{
int fileCounter = 0;
foreach (HttpPostedFile file in PackagingPhoto1.PostedFiles)
{
第 2 步:
去掉 for 循环,只在那个位置递增 fileCounter 一次。
else
{
fileCounter++;
string Path = Server.MapPath("~/IncomingPhotos/" + GridView1.SelectedRow.Cells[1].Text + "." + GridView1.SelectedRow.Cells[2].Text +
".ReceivingPhoto" + fileCounter + extension);
file.SaveAs(Path);
Label9.Text = "Photos were successfully uploaded.";
Label9.ForeColor = System.Drawing.Color.Green;
}
我可以用这样输入的代码让它正确上传所有 1、2 或 3 个文件--
else{
string Path = Server.MapPath("~/IncomingPhotos/" + file.FileName);
file.SaveAs(Path);
}
但我需要能够用这样的名称保存每个文件--
1.1003.IncomingPhoto1
1.1003.IncomingPhoto2
1.1003.IncomingPhoto3
1表示order_id,1003表示customer_id.
表单代码:
<table>
<tr>
<td align="center" class="auto-style2" colspan="2">
<asp:FileUpload ID="PackagingPhoto1" runat="server" Width="437px" AllowMultiple="True" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="packagingPhotos" runat="server" OnClick="packagingPhotos_Click" Text="Upload
Photos" Width="175px" />
</td>
</tr>
</table>
隐藏代码:
protected void packagingPhotos_Click(object sender, EventArgs e)
{
if (PackagingPhoto1.HasFile && PackagingPhoto1.PostedFiles.Count <= 3)
{
foreach (HttpPostedFile file in PackagingPhoto1.PostedFiles)
{
string extension = Path.GetExtension(file.FileName);
if (extension.ToLower() != ".jpeg" && extension.ToLower() != ".jpg" && extension.ToLower() != ".png")
{
Label9.Text = "Only files with .jpg, .jpeg, or .png extension are allowed.";
Label9.ForeColor = System.Drawing.Color.Red;
}
else
{
int photoSize = file.ContentLength;
if (photoSize > 5242880)
{
Label9.Text = "Maximum file size (5MB) exceeded!";
Label9.ForeColor = System.Drawing.Color.Red;
}
else
{
for (int i = 1; i < 3; i++)
{
string Path = Server.MapPath("~/IncomingPhotos/" + GridView1.SelectedRow.Cells[1].Text + "." + GridView1.SelectedRow.Cells[2].Text + ".ReceivingPhoto" + i + extension);
file.SaveAs(Path);
}
Label9.Text = "Photos were successfully uploaded.";
Label9.ForeColor = System.Drawing.Color.Green;
}
}
}
}
else
{
Label9.Text = "Please select at least 1 but no more than 3 files.";
Label9.ForeColor = System.Drawing.Color.Red;
}
}
您的问题是您在错误的位置实例化了计数器变量 (i),并且您有一个不必要的 for 循环。
第 1 步:
在 foreach 循环上方的行中声明并将计数器变量实例化为 0。当你在使用它时,称它为比 "i" 更好的东西,比如 "fileCounter"
if (PackagingPhoto1.HasFile && PackagingPhoto1.PostedFiles.Count <= 3)
{
int fileCounter = 0;
foreach (HttpPostedFile file in PackagingPhoto1.PostedFiles)
{
第 2 步:
去掉 for 循环,只在那个位置递增 fileCounter 一次。
else
{
fileCounter++;
string Path = Server.MapPath("~/IncomingPhotos/" + GridView1.SelectedRow.Cells[1].Text + "." + GridView1.SelectedRow.Cells[2].Text +
".ReceivingPhoto" + fileCounter + extension);
file.SaveAs(Path);
Label9.Text = "Photos were successfully uploaded.";
Label9.ForeColor = System.Drawing.Color.Green;
}