【问题标题】:Get the next file name to upload获取下一个要上传的文件名
【发布时间】:2013-05-04 09:50:46
【问题描述】:

我有以下 C# 代码(关于将文件上传到服务器)

for (int i = 0; i < Request.Files.Count-1; i++)
                {
                    if (Request.ContentLength != 0)
                    {
                        int Size = Request.Files[i].ContentLength / 1024;
                        if (Size <= 512)
                        {
                            string LocalFile = Request.Files[i].FileName;
                            int dot = LocalFile.LastIndexOf('.');
                            string FileType = LocalFile.Substring(dot + 1);
                            if (FileType == "gif" || FileType == "jpg" || FileType == "png" || FileType == "GIF" || FileType == "JPG" || FileType == "PNG")
                            {
                                int LastIndex = LocalFile.LastIndexOf(@"\") + 1;
                                string File = LocalFile.Substring(LastIndex, LocalFile.Length - LastIndex);
                                string Path = Server.MapPath(" ../images/tracks") + "..\\" + File;
                                Request.Files[i].SaveAs(Path);
                                if (i != Request.Files.Count - 1)
                                    ImageList += string.Format("images/tracks/{0}|", File);
                                else { ImageList += string.Format("images/tracks/{0}", File); }
                            }
                        }
                        else
                        {
                            Response.Write("The file is too big !");
                        }
                    }
                    else
                    {
                        Response.Write("Unknown Error !");
                    }
                }

问题是文件上传字段不止一个。

我想创建条件,它将检查文件 [i] 之后是否有文件(如果文件 [i+1] 为空,则检查) 如果是,程序将执行此代码:ImageList += string.Format("images/tracks/{0}", File);

其他:ImageList += string.Format("images/tracks/{0}|", File);

我的问题是条件应该是什么样子?

希望得到帮助,谢谢!

【问题讨论】:

  • 您应该不需要对之后的文件进行检查,因为您已经在循环遍历整个文件长度 for (int i = 0; i &lt; Request.Files.Count-1; i++) - 还是我误解了这个问题?

标签: asp.net file file-upload if-statement


【解决方案1】:

只需删除您的条件(如果)并添加所有带有结束字符的字符串。在循环结束时,您可以删除最后一个字符,如果它是“|”。

在您的代码上:

for (int i = 0; i < Request.Files.Count-1; i++)
            {
                if (Request.ContentLength != 0)
                {
                    int Size = Request.Files[i].ContentLength / 1024;
                    if (Size <= 512)
                    {
                        string LocalFile = Request.Files[i].FileName;
                        int dot = LocalFile.LastIndexOf('.');
                        string FileType = LocalFile.Substring(dot + 1);
                        if (FileType == "gif" || FileType == "jpg" || FileType == "png" || FileType == "GIF" || FileType == "JPG" || FileType == "PNG")
                        {
                            int LastIndex = LocalFile.LastIndexOf(@"\") + 1;
                            string File = LocalFile.Substring(LastIndex, LocalFile.Length - LastIndex);
                            string Path = Server.MapPath(" ../images/tracks") + "..\\" + File;
                            Request.Files[i].SaveAs(Path);
                            //if (i != Request.Files.Count - 1)
                                ImageList += string.Format("images/tracks/{0}|", File);
                            //else { ImageList += string.Format("images/tracks/{0}", File); }
                        }
                    }
                    else
                    {
                        Response.Write("The file is too big !");
                    }
                }
                else
                {
                    Response.Write("Unknown Error !");
                }
            }
           //Remove the last character
           if (ImageList.EndsWith("|")) ImageList = ImageList.Remove(ImageList.Length - 1, 1);
}

【讨论】:

  • 非常感谢!这就是我要找的!
猜你喜欢
  • 1970-01-01
  • 2013-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-28
  • 1970-01-01
相关资源
最近更新 更多