Home > Enterprise >  Filename overwrites the uploaded attachments
Filename overwrites the uploaded attachments

Time:01-26

The file name is generated by Po Invoice in the sql table. When user attach a pdf it overwrites the existing file name that has same po invoice. Is there anyway i could create the unique id filename.

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 = "";
    }
}

I have tried

Guid.NewGuid()   ".pdf";,

it still does the same thing.

CodePudding user response:

The simple way would be to just take the file name, and add -1, then -2, then -3

So,

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

So, then say a function like this:

Edit: Updated code

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;
}

So, now your code becomes this:

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

 sFileToSave = GetServerFileNext(sFileToSave);

 tPT.FileName = Path.GetFileName(sFiletoSave);

So, you setup the full path and file name.

You then check if it exists, and if not, then add -1, then -2 etc. etc.

Edit2: So, code would look like this (warning: air code)

        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);
  • Related