asp.net 文件名覆盖上载的附件

k5hmc34c  于 2023-01-27  发布在  .NET
关注(0)|答案(1)|浏览(127)

文件名是由采购订单+发票在sql表中生成的。当用户附加一个pdf文件时,它会覆盖具有相同采购订单+发票的现有文件名。无论如何,我可以创建唯一ID文件名吗?

public partial class upload : System.Web.UI.Page
{
    string mID;

    DataBaseDataContext dataBaseDataContext = new DataBaseDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnExit_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }

    protected void SUbmit(object sender, EventArgs e)
    {
        try
        {
            var ext = FileUploadtoServer.FileName.Substring(FileUploadtoServer.FileName.IndexOf('.') + 1, 3);
            if (ext.ToLower() != "pdf")
            {
                Response.Write("<script>alert('Please select a pdf File!')</script>");
                return;
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('Please select a pdf File!')</script>");
            return;
        }

        if (PO_Number.Text.Equals("") || HeadCode.Text.Equals("") || Manufacture.Text.Equals("") || Description.Text.Equals("") || Invoice.Text.Equals(""))
        {
            Response.Write("<script>alert('Please Fill All Fields!')</script>");
        }

        TPT tPT = new TPT();
        tPT.Invoice_No = Invoice.Text;
        tPT.PO_Number = PO_Number.Text;
        tPT.Item_Code = Manufacture.Text;
        tPT.Heat_Code = HeadCode.Text;
        tPT.Description = Description.Text;
        tPT.FileName = PO_Number.Text + Invoice.Text + ".pdf";
        dataBaseDataContext.TPTs.InsertOnSubmit(TPT);
        dataBaseDataContext.SubmitChanges();

        String path = Server.MapPath("Attachments/" + PO_Number.Text + Invoice.Text + ".pdf");
        FileUploadtoServer.SaveAs(path);
        Response.Write("<script>alert('Successfully Inserted!')</script>");
        Invoice.Text = "";
        Manufacture.Text = "";
        HeadCode.Text = "";
        Description.Text = "";
        PO_Number.Text = "";
    }
}

我试过了

Guid.NewGuid() + ".pdf";,

它仍然做同样的事情。

ruyhziif

ruyhziif1#

最简单的方法是获取文件名,然后依次添加-1、-2和-3
所以,

mydoc.pdf
   mydoc-1.pdf
   mydoc-2.pdf
   etc. etc.

那么,再假设一个这样的函数:

编辑:更新代码

string GetServerFilenext(string strFileFullPath)
{
    string strFileToCheck = "";
    for (int i = 0; i < 100; i++)
    {
        if (i == 0)
            strFileToCheck = strFileFullPath;
        else
            strFileToCheck =
                Path.GetDirectoryName(strFileFullPath) + 
                @"\" + Path.GetFileNameWithoutExtension(strFileFullPath) +
                "-" + i.ToString() + Path.GetExtension(strFileFullPath);

        if (!File.Exists(strFileToCheck))
            break;
    }
    return strFileToCheck;
}

所以,现在你的代码变成这样:

string sFileToSave = 
       Server.MapPath(@"~/Attachments/" + PO_Number.Text + Invoice.Text + ".pdf");

 sFileToSave = GetServerFileNext(sFileToSave);

 tPT.FileName = Path.GetFileName(sFiletoSave);

因此,您需要设置完整的路径和文件名。
然后检查它是否存在,如果不存在,则添加-1,然后添加-2等。

Edit2:因此,代码将如下所示(警告:航空代码)

string sFileToSave =
            Server.MapPath(@"~/Attachments/" + PO_Number.Text + Invoice.Text + ".pdf");

        sFileToSave = GetServerFileNext(sFileToSave);

        TPT tPT = new TPT();
        tPT.Invoice_No = Invoice.Text;
        tPT.PO_Number = PO_Number.Text;
        tPT.Item_Code = Manufacture.Text;
        tPT.Heat_Code = HeadCode.Text;
        tPT.Description = Description.Text;
        tPT.FileName = Path.GetFileName(sFileToSave);
        dataBaseDataContext.TPTs.InsertOnSubmit(TPT);
        dataBaseDataContext.SubmitChanges();

        FileUploadtoServer.SaveAs(sFileToSave);

相关问题