循环浏览3张照片,并以递增的名称保存每张照片。

问题描述 投票:0回答:1

我可以让它正确上传1、2或3个文件,输入的代码是这样的--。

else{
    string Path = Server.MapPath("~/IncomingPhotos/" + file.FileName);
    file.SaveAs(Path);
    }

但我需要能够用这样的名字保存每个文件-------。

1.1003.IncomingPhoto1。

1.1003.IncomingPhoto2。

1.1003.IncomingPhoto3。

1表示订单ID,1003表示客户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) exceeeded!";
                                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;
                }
            }
save-as
1个回答
0
投票

你的问题是你在错误的地方实例化了你的计数器变量(i),你有一个不必要的for循环。

第1步:在foreach循环上面的那行声明并实例化你的计数器变量为0。 同时,给它起一个比 "i "更好的名字,比如 "fileCounter"。

        if (PackagingPhoto1.HasFile && PackagingPhoto1.PostedFiles.Count <= 3)
        {
            int fileCounter = 0;
            foreach (HttpPostedFile file in PackagingPhoto1.PostedFiles) 
            {

第二步:摆脱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;
                        }
© www.soinside.com 2019 - 2024. All rights reserved.