When uploading file to server, how to check for the existence of the file RRS feed

  • Question

  • User1754914522 posted

    Hi all,

      File upload is not a required field, so if it has file then it needs to be checked that already exists on the server side, if not then it should write to the server. I used the code whcih was used before, but not sure how to to check if the upload has file.


    physicalFolder = "C://Sample/temp//";

    string Filename = FileUpload1.FileName;

    if (FileUpload1.HasFile)


    string PathtoCheck = physicalFolder + FileUpload1.FileName;

    if (System.IO.File.Exists(PathtoCheck) )



    FileUpload1.SaveAs(physicalFolder + FileUpload1.FileName);


    on the client side, am using this


    td align="center" class="style3" colspan



    if (System.IO.File.Exists(PathtoCheck)) %>

    <%{ %>

    <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Submit" OnClientClick="return confirm('Are you certain you want to do what you want to do?');"/><%} %> />

    </td>, but this gives an error saying PathToCheck doesn't exist

    Thanks for any help


    Tuesday, February 14, 2012 12:52 PM


  • User-1226263862 posted

    If PathToCheck isn't a global variable then you cannot access it elsewhere.  To me it appears it is only available in that method so you can't use in in your aspx page like you show.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 14, 2012 3:21 PM