【问题标题】:Check for duplicates files on upload in c#在c#中检查上传的重复文件
【发布时间】:2016-07-27 11:06:28
【问题描述】:

使用 C# 中的这个 asp 网页上传文件,我需要检查重复文件。

我接受在服务器上上传的 3 个文件。

此代码有效并且未上传重复文件,但警报弹出窗口仅针对第一个要上传的重复文件打开,即使重复文件更多。

有什么问题?

下面是我的代码,提前谢谢你。

if (File.Exists(upload.FileName))
{
    DirectoryInfo objDir = new DirectoryInfo(Server.MapPath("\\images\\"));

    FileInfo[] objFI = objDir.GetFiles("*.*");

    int iFileCnt = 0;

    if (objFI.Length > 0)
    {
        foreach (FileInfo file in objFI)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "alert('This file exists " + upload.FileName + "');", true);
            iFileCnt += 1;
        }
    }                        
}

【问题讨论】:

  • 你能把整个函数贴出来吗?就这样,我看到了很多无意义的东西。

标签: c# asp.net file-upload


【解决方案1】:

"Msg" 更改为"Msg" + iFileCnt。这会更改每次迭代的键。

Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg" + iFileCnt, "alert('This file exists " + file.Name + "');", true);

现在警报在循环中执行不止一次。

【讨论】:

  • 感谢您的回复。现在在重复文件中,我有多个打开的警报弹出窗口,但文件名相同...
  • @AntonioMailtraq 也许你想打印而不是upload.FileName this: file.Name in alert.
  • 非常感谢您的帮助!
【解决方案2】:

这样的事情怎么样?

List<string> Filenames = new List<string>()
{
    "File1.jpg",
    "File2.jpg"
    //etc.
};

foreach(string s in Filenames)
{
    Upload(s);
}

private void Upload(string filename)
{
    string directory = @"\\path\\to\\directory";
    string fullpath = string.Format(@"{0}\{1}", directory, filename);

    if(File.Exists(fullpath))
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg",     "alert('This file exists " + filename + "');", true);
    }
}

【讨论】:

    猜你喜欢
    • 2017-08-17
    • 2012-03-17
    • 2016-12-01
    • 2020-01-07
    • 2013-03-16
    • 1970-01-01
    • 2018-01-01
    • 2018-02-04
    • 2012-05-14
    相关资源
    最近更新 更多