Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Return message to user if file already exists

Return message to user if file already exists

Scheduled Pinned Locked Moved ASP.NET
databasehtmlsysadminquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hamiltonian13
    wrote on last edited by
    #1

    Hi all, I am developing an application that allows users to upload/download/delete files. I have it working but I am struggling to add the following function. Check if the file already exists in the specified folder, if the file exists already, return a message telling the user to choose another name. if the file doesn't exist, save the file in the specified folder. This is my controller code:

    public ActionResult UploadFiles(int? id)
    {
    CI cI = db.Database.Find(id);
    foreach (string upload in Request.Files)
    {

                if (Request.Files\[upload\].FileName != "")
                    
                {
                    string path = Server.MapPath("~/App\_Data/uploads/" + id + "/");
                    string filename = Path.GetFileName(Request.Files\[upload\].FileName);
                    Response.Write(path);
                  
                    Request.Files\[upload\].SaveAs(Path.Combine(path, filename));
               
                }
            }
           
            return View("Upload");
        }
    

    The upload view:

    @{
    ViewBag.Title = "Upload";
    }

    Upload

    $(document).ready(function () {
        $('#btnUploadFile').on('click', function () {
            var data = new FormData();
            var files = $("#fileUpload").get(0).files;
            // Add the uploaded image content to the form data collection
            if (files.length > 0) {
                data.append("UploadedImage", files\[0\]);
            }
            // Make Ajax request with the contentType = false, and procesDate = false
            var ajaxRequest = $.ajax({
                type: "POST",
                url: "",
                contentType: false,
                processData: false,
                data: data
            });
    
            ajaxRequest.done(function (xhr, textStatus) {
                @ViewBag.Message
            });
        });
    });
    

    @Html.ActionLink("Documents", "Downloads")

    F 1 Reply Last reply
    0
    • H Hamiltonian13

      Hi all, I am developing an application that allows users to upload/download/delete files. I have it working but I am struggling to add the following function. Check if the file already exists in the specified folder, if the file exists already, return a message telling the user to choose another name. if the file doesn't exist, save the file in the specified folder. This is my controller code:

      public ActionResult UploadFiles(int? id)
      {
      CI cI = db.Database.Find(id);
      foreach (string upload in Request.Files)
      {

                  if (Request.Files\[upload\].FileName != "")
                      
                  {
                      string path = Server.MapPath("~/App\_Data/uploads/" + id + "/");
                      string filename = Path.GetFileName(Request.Files\[upload\].FileName);
                      Response.Write(path);
                    
                      Request.Files\[upload\].SaveAs(Path.Combine(path, filename));
                 
                  }
              }
             
              return View("Upload");
          }
      

      The upload view:

      @{
      ViewBag.Title = "Upload";
      }

      Upload

      $(document).ready(function () {
          $('#btnUploadFile').on('click', function () {
              var data = new FormData();
              var files = $("#fileUpload").get(0).files;
              // Add the uploaded image content to the form data collection
              if (files.length > 0) {
                  data.append("UploadedImage", files\[0\]);
              }
              // Make Ajax request with the contentType = false, and procesDate = false
              var ajaxRequest = $.ajax({
                  type: "POST",
                  url: "",
                  contentType: false,
                  processData: false,
                  data: data
              });
      
              ajaxRequest.done(function (xhr, textStatus) {
                  @ViewBag.Message
              });
          });
      });
      

      @Html.ActionLink("Documents", "Downloads")

      F Offline
      F Offline
      F ES Sitecore
      wrote on last edited by
      #2

      When you have the target file name ("Path.Combine(path, filename)") use File.Exists to check if it exists File.Exists Method (String) (System.IO)[^] If it does exists you can return Json from your action, something like {success:false}, and in your ajax call you can examine the result of the call and if it is a json object where success==false then you know the upload didn't work so you can show a message, otherwise do what you normally do with the result. You'll maybe want to run through all the files first to check for existing files and return the failure message if any exist and if not run through them again to save them.

      H 1 Reply Last reply
      0
      • F F ES Sitecore

        When you have the target file name ("Path.Combine(path, filename)") use File.Exists to check if it exists File.Exists Method (String) (System.IO)[^] If it does exists you can return Json from your action, something like {success:false}, and in your ajax call you can examine the result of the call and if it is a json object where success==false then you know the upload didn't work so you can show a message, otherwise do what you normally do with the result. You'll maybe want to run through all the files first to check for existing files and return the failure message if any exist and if not run through them again to save them.

        H Offline
        H Offline
        Hamiltonian13
        wrote on last edited by
        #3

        Thanks a mill! Got the basic function working using:

        System.Diagnostics.Debug.Write(System.IO.File.Exists(Path.Combine(path, filename)) ? "File exists." : "File does not exist.");

        You are a star. I know I should be able to find this info by myself but I am very very early in my development so sometimes it is hard to know what to look for. Thanks!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups