繁体   English   中英

如何在全局数组中分配每个文件名?

[英]how can i assign each file name in global array?

我使用多个文件上传,我想使用foreach为名为img的全局数组分配每个文件名,该如何在asp.net/c#中实现呢?

string[] img={""};

foreach (string s in Request.Files) {
    HttpPostedFile file = Request.Files[s];
    int fileSizeInBytes = file.ContentLength;
    string fileName = file.FileName;
    string fileExtension = "";

    if (!string.IsNullOrEmpty(fileName)) {
        fileExtension = Path.GetExtension(fileName);
        file.SaveAs(filename);
    }
}

为此,我将使用List<string>

List<string> img = new List<string>();

foreach (string s in Request.Files)
{
    HttpPostedFile file = Request.Files[s];
    int fileSizeInBytes = file.ContentLength;
    string fileName = file.FileName;
    string fileExtension = "";

    if (!string.IsNullOrEmpty(fileName))
    {
        img.add(fileName);
        fileExtension = Path.GetExtension(fileName);
        file.SaveAs(filename);
    }

这是一个很好的解释,为什么List<T>总是总是比在C#中使用array更好: https : //softwareengineering.stackexchange.com/a/221897

如果您为我们要完成的任务提供更好的解释,那么我可能会对此做出改进。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM